Parsing a text file

I am trying to parse a txt file that contains names in the format:

"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH",... 

This is the code I wrote:


 #include <stdio.h> // Names scores int problem22() { FILE *f = fopen("names.txt", "r"); char name[100]; fscanf(f, "\"%[^\"]s", name); printf("%s\n", name); // MARY fscanf(f, "\"%[^\"]s", name); printf("%s\n", name); // , fscanf(f, "\"%[^\"]s", name); printf("%s\n", name); // PATRICIA return 0; } int main() { problem22(); return 0; } 

Each alternate call to fscanf gives me a name, and the other in getting a comma. I tried several formats, but I can not figure out how to do this.

Can someone help me with the correct format?

+8
c parsing scanf
source share
4 answers

Changing the input format string to "%*[,\"]%[^\"]" will do what you want:

 fscanf(f, "%*[,\"]%[^\"]", name); printf("%s\n", name); // MARY fscanf(f, "%*[,\"]%[^\"]", name); printf("%s\n", name); // PATRICIA fscanf(f, "%*[,\"]%[^\"]", name); printf("%s\n", name); // LINDA 

%* just skips the corresponding input.

+3
source share

I always like to use the strtok() or strtok_r() function to parse the file. (or prefer to use some csv library).

But just for fun, I wrote the code, maybe you like it, I do not send the code in my answer, but I check @ codepad for output, it works only for a certain format.

Using strtok ()

The correct approach looks to me as follows:

 int main(){ // while(fp, csv, sizeof(csv)){ // First read into a part of file into buffer char csv[] = "\"MARY\",\"PATRICIA\",\"LINDA\",\"BARBARA\",\"ELIZABETH\""; char *name = "", *parse = csv; while(name = strtok(parse, "\",")){ printf(" %s\n", name); parse = NULL; } return 0; } // end while 

Mark the code page for the output:

  MARY PATRICIA LINDA BARBARA ELIZABETH 

As I suggest in the second code, draw an outer loop to read the lines from the file into a temporary buffer, and then apply the strtok () code as shown above: while(fgets(fp, csv, sizeof(csv))){ use strtok code}

+3
source share

You must use fseek () .

This code works successfully:

 #include <stdio.h> #include <string.h> int problem22() { FILE *f = fopen("names.txt", "r"); char name[100]; int pos = 0, maxnames = 4, n; for(n = 0; n <= maxnames; n++) { fseek(f, pos, 0); fscanf(f, "\"%[^\"]s", name); printf("%s\n", name); pos += (strlen(name) + 3); } return 0; } int main() { problem22(); return 0; } 
+2
source share

You can use strtok() read the whole line and split it into tokens using the line delin ","

 #include <stdio.h> #include <stdlib.h> #include <string.h> // Names scores int problem22() { FILE *f = fopen("file", "r"); char *tok=NULL; char name[100]; fscanf(f,"%s",name); printf("string before strtok(): %s\n", name); tok = strtok(name, ","); while (tok) { printf("Token: %s\n", tok); tok = strtok(NULL, ","); } return 0; } int main() { problem22(); return 0; } 

Note. The strtok() uses a static buffer when parsing, so it is not thread safe. Use strtok_r() if that matters to you.

see man strtok_r

+1
source share

All Articles