When I run the following code, I get a "Segmentation fault" at fprintf(outfile, "%s", inputline[j]);
.
I am unable to understand what is the cause for the error. I am relatively new to C, can someone please help me resolve the error?
void test(char *inputline) {
FILE *outfile = fopen("results.txt", "w");
if (!outfile) {
perror("Error while opening file: ");
} else {
for (int j = 0; j < 20; ++j) { // I only want to be write the first 20 characters to the file that is why I have the iteration till only 20 and added [j], is that correct way to do it?
fprintf(outfile, "%s", inputline[j]);
}
}
}
//Function call
...
char inputline[40] = "hello world 123 456"; //passed to the function above
test(inputline);
See Question&Answers more detail:os