c - Mental block on fprintf string termination -
i @ total loss one. can't figure out why isn't working. simple character array null terminator - except when output it, doesn't terminate!
int file_create(const char *path) { //trying trap situations path starts /.goutputstream char path_left_15[16]; strncpy(path_left_15, path, 15); printf("%d\n", strlen("/.goutputstream")+1); path_left_15[strlen("/.goutputstream")+1] = '\0'; printf("%d\n", strlen(path_left_15)); printf("path_left_15: %s\n", path_left_15); //continue on... }
this output:
> 16 > 16 >/.goutputstream\b7<random memory stuff>
i can't figure out why isn't terminating correctly. i've tried making array longer, same result every time. i'm losing mind!
anyone see it? thanks.
you out of bound array. instead of path_left_15[strlen("/.goutputstream")+1] = '\0';
try path_left_15[15] = '\0';
you truncate string safe when printing.
Comments
Post a Comment