Search code examples
cscanfstring-interpolation

Reading by scanf line by line


I need to use scanf to read a text file input into stdin. I want to read the text file line by line and then assign each values in the line to their values.

This is the text file
38 5 35 3099 48 222 16 4 1 Ronaldo 2 33 2572 22 97 7 6 0 Neymar 1 38 3375 43 191 18 4 0 Messi 13 37 2472 22 80 1 6 0 Griezmann

The first line is the number of max games. Then I want to read the next lines and assign it to the variables. Like number = 5, goals = 35, .... Then on to the next line and read the same way. Number = 2, goals = 33, ....

How would I do this?


Solution

  • You can use fscanf to read from the text file (as you have fixed format of data) -

    char name[100];
    int a,b,c,d,e,f,g,h;           // sorry for such variable names 
    while(fscanf(fp,"%d %d %d %d %d %d %d %d %99s",&a,&b,&c,&d,&e,&f,&g,&h,name)==9)   // fp be file pointer