Wednesday, 15 April 2015

string - Concatenating file extension to base name in C -



string - Concatenating file extension to base name in C -

for reason next not working:

int i; for(i = 1; < argc; i++) // create thread each dataset. { filename = strcat(argv[i], ".sdx"); // concatenate file-extension '.sdx' basename. pthread_attr_init(&attr); // set attribute of thread (default). pthread_create(&tid[i], &attr, start_routine, filename); // create thread. pthread_join(tid[i],null); // bring together thread after completed. }

it works if pass in 1 file, more gives segmentation fault. dont understand, works if dont concatenate file extension, , instead pass in total filename (including extension) command line argument.

you should not modify argv[i] directly. re-create local buffer instead.

int i; for(i = 1; < argc; ++i) { char *filename = malloc(strlen(argv[i]) + 4 + 1); sprintf(filename, "%s.sdx", argv[i]); pthread_attr_init(&attr); // set attribute of thread (default). pthread_create(&tid[i], &attr, start_routine, filename); // create thread. pthread_join(tid[i],null); // bring together thread after completed. free(filename); }

c string multithreading strcat

No comments:

Post a Comment