I have a text file with the following contents:
"abc","def","ghi"
The following works to read the contents of the file correctly:
int main() { char name[1024] = {0}; FILE *file = fopen("file.txt", "r"); while(1) { if (fscanf(file, " %[\",]s ", name) == EOF) break; if (fscanf(file, " %[a-zA-Z]s ", name) == EOF) break; printf("%s\n", name); } return 0; }
However, the following fails:
int main() { char name[1024] = {0}, garbage[5]; FILE *file = fopen("file.txt", "r"); while(1) { if (fscanf(file, " %[\",]s%[a-zA-Z]s ", garbage, name) == EOF) break; printf("%s\n", name); } return 0; }
I am using MSVC ++ 08. What am I missing? I am looking for a solution with single fscanf() in a while .
c input scanf
Donotalo
source share