Answer the question
In order to leave comments, you need to log in
How do I properly concatenate three strings and make a system call?
Good afternoon. There is this code in C:
char filenames[1000];
memset(filenames, 0, 1000);
printf("Enter filenames:\n");
read(0, filenames, 1000);
printf(filenames);
char* part1 = "file ";
char* part2 = "| grep 'C source' | wc -1";
char* result = malloc(strlen(part1) + strlen(filenames) + strlen(part2) + 1);
memset(result, 0, strlen(part1) + strlen(filenames) + strlen(part2) + 1);
memcpy(result, part1, strlen(part1));
memcpy(result, filenames, strlen(filenames));
memcpy(result, part2, strlen(part2));
printf(result);
system(result);
Answer the question
In order to leave comments, you need to log in
When doing memcpy, the pointer to result must be offset by the size of the previous copied line. Now you just copy the next line, overwriting the previous copy.
memcpy(result + strlen(part1), filenames, strlen(filenames));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question