I have a program called test.c, and an input file called test.in. I’m trying to read two strings from the input file and output them to the console. I’m running Windows 7 but I’m compiling with MinGW using gcc.
This is my file test.c:
#include <stdio.h>
main() {
FILE *fin = fopen ("test.in", "r");
char *one, *two;
fscanf(fin, "%s %s", one, two);
printf("%s\n%s", one, two);
fclose(fin);
return 0;
}
And this is my file test.in :
ONE
TWO
I go to MinGW, run “gcc -o test.exe test.c”, everything compiles fine, but when I run test.exe, a pop-up comes up and says “test.exe has stopped working. Windows is trying to find a solution to the problem…”
Does anyone know why this is?
fscanfis writing to the uninitialised pointers you pass it. The effects of this are undefined but a crash is likely. You need to allocate your char arrays before calling it: