I want to use fscanf() to read a whole line(including space) from a file. The file looks like this:
//data.txt
lek yuen
3
wo che
5
wo che
8
wo che
5
wo che
7
sha kok
0
hin keng
9
lung hang
8
sha kok
2
sha kok
4
lung hang
8
When I use buffer
to store it and use printf()
to show it, the result lek yuen
just pops out less than 1 second and the whole cmd window disappeared. In the compiler, it shows that : Process finished with exit code 0 .
Below is my code:
int main() {
FILE *fp=fopen("data.txt","r");
char buffer[255];
if(fp==NULL){
perror("File not exist!\n");
exit(1);
}
fscanf(fp,"%[^\n]s",&buffer);
printf("%s",buffer);
fclose(fp);
return 0;
}
Does anyone know what happened? Thank you very much!