#include <stdio.h>
#include <string.h>
int main() {
char tab[2]={"12"};
FILE *outfile;
char *outname = "/home/dir/";
printf("%s", strcat(outname,tab));
outfile = fopen(strcat(outname,btab), "w");
if (!outfile) {
printf("There was a problem opening %s for writing\n", outname);
}
}
I have this error: Segmentation Fault.
How can I fix it?
At least two errors:
You’d better use
tab[3]or even bettertab[]— you need one extra char for the terminating NUL character.Also,
creates a constant string in the data segment of the executable — it can’t be overwritten, since
strcatis using its first parameter to concatenate the two strings. So whenstrcat()tries to do so, it segfaults. Useinstead.