I've a text file with following contents:
"abc","def","ghi"
The following works to read the file contents properly:
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'm using MSVC++ 08. What am I missing? I'm looking for a solution with single fscanf()
in the while
loop. Thanks in advanced.