Thread: File scan with specific format and retrieve the data on following numbers in C

  1. #1
    Registered User
    Join Date
    Jun 2022
    Posts
    1

    File scan with specific format and retrieve the data on following numbers in C

    So I'm new to C programming and have some trouble figuring out how to file scan and input each different data into each variable. I already think file scan with fscanf, fgets or fread, but i don't know how to do it. This is the code that i already write:
    Code:
     #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    struct testfile
        {
            char name[20];
            int number;
            int height;
            float weight;
        }file[100];
    
    
    int main ()
    {
        char buffer[100];
        char choose='T';
        int x,y,z;
        FILE *fp;
        fp=fopen("File.txt","r");
    
    
        /* Fscanf, but fail
        for(x=0;x<100;x++)
        {
            fscanf(fp,"%d %[^\n] %d %f",&file[x].number,file[x].name,&file[x].height,&file[x].weight);
            printf("%d %s %d %f\n",file[x].number,file[x].name,file[x].height,file[x].weight);
        }*/
    
    
        /* fgets, same fail too
        while(!feof(fp))
        {
            fgets(buffer,sizeof buffer,fp);
            sscanf(buffer,"%d",&file[x].number);
            fgets(buffer,sizeof buffer,fp);
            sscanf(buffer,"%[^\n]",file[x].name);
            fgets(buffer,sizeof buffer,fp);
            sscanf(buffer,"%d",&file[x].height);
            fgets(buffer,sizeof buffer,fp);
            sscanf(buffer,"%f",&file[x].weight);
            printf("%d %s %d %f\n",file[x].number,file[x].name,file[x].name,file[x].height,file[x].weight);
            x++;
        }
        */
        
        /*After the file is success scanned and save into each variable, the user can choose what number from the data and retrieve the following data on that number
        while (choose !='Y')
        {
        printf("Choose Data: ");
        scanf("%d",&file[x].number);
        printf("Data number : %d\nName : %s\nHeight : %d\nWeight : %.2f\n",file[x].number,file[x].name,file[x].name,file[x].height,file[x].weight);
        x++;
        printf("Get Another Data? (Y/N) : ");
        scanf("%c",&choose);
        }*/
        fclose(fp);
        return 0;
    }
    File.txt content:
    Code:
    No	Name		       			Height (CM)		Weight (KG)          	1	Josh Broclin				    175       		83 
    2	Ryan Andreas            		184       		98
    3	Tom Norton	           		    162      		111.6
    4	Harry Syd	              		190      		68
    5	Hayu Beurang		       		181      		75
    6	Jeff Rick               		169       		108
    7	Asley Thomas				    179      		104
    This is the attempt using fscanf (not mixed with fgets)
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    struct testfile
        {
            char name[20];
            int number;
            int height;
            float weight;
        }file[100];
    
    
    int main ()
    {
        char buffer[100];
        char choose='T';
        int x,y,z;
        FILE *fp;
        fp=fopen("File.txt","r");
    
    
         //Fscanf, but fail
        for(x=0;x<10;x++)
        {
            fscanf(fp,"%d %[^\n] %d %f",&file[x].number,file[x].name,&file[x].height,&file[x].weight);
            printf("%d %s %d %f\n",file[x].number,file[x].name,file[x].name,file[x].height,file[x].weight);
        }
    
    
        fclose(fp);
        return 0;
    
    
    }
    And this is the output:
    Code:
    0  0 0.0000000  0 0.000000
    0  0 0.000000
    0  0 0.000000
    0  0 0.000000
    0  0 0.000000
    0  0 0.000000
    0  0 0.000000
    0  0 0.000000
    0  0 0.000000
    
    
    Process returned 0 (0x0)   execution time : 0.020 s
    Press any key to continue.
    This one is using fgets (not mixed with fscanf)
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    struct testfile
        {
            char name[20];
            int number;
            int height;
            float weight;
        }file[100];
    
    
    int main ()
    {
        char buffer[100];
        char choose='T';
        int x,y,z;
        FILE *fp;
        fp=fopen("File.txt","r");
    
    
        while(!feof(fp))
        {
            fgets(buffer,sizeof buffer,fp);
            sscanf(buffer,"%d",&file[x].number);
            fgets(buffer,sizeof buffer,fp);
            sscanf(buffer,"%[^\n]",&file[x].name);
            fgets(buffer,sizeof buffer,fp);
            sscanf(buffer,"%d",&file[x].height);
            fgets(buffer,sizeof buffer,fp);
            sscanf(buffer,"%f",&file[x].weight);
            printf("%d %s %d %f\n",file[x].number,file[x].name,file[x].name,file[x].height,file[x].weight);
            x++;
        }
        fclose(fp);
        return 0;
    
    
    }
    The output for fgets:
    Code:
    ```
    538976309 1     Josh Broclin                            175   ☻ 4225408 0.000000
    153100320 5     Hayu Beurang                    ♠ 4225440 0.000000
    
    
    Process returned 0 (0x0)   execution time : 0.105 s
    Press any key to continue.
    Both method that i currently using give a wrong output.
    After the file is scanned, it can retrieve data information depend on what number the user choose, for example:
    Code:
    Choose Data: 3
    Data number :3
    Name : Tom Norton
    Height : 162
    Weight : 111.6
    Get Another Data? (Y/N) :

    So how to solve this? I've been thinking about this problem and its solution, but can't find the solution. It really disturbs me whenever I want to sleep this problem comes again and figure out how to solve it. Thank you.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Since your file appears to have a "header"
    ("No Name Height (CM) Weight (KG)")
    , you will need to do something with this "data".

    Also it would be easier if your first true data item was not on the same line as the header.

    Code:
    No  Name                        Height (CM)     Weight (KG)             // HEADER LINE
    1   Josh Broclin                    175             83 
    2   Ryan Andreas                    184             98
    3   Tom Norton                      162             111.6
    4   Harry Syd                       190             68
    5   Hayu Beurang                    181             75
    6   Jeff Rick                       169             108
    7   Asley Thomas                    179             104
    With this format it will be easier to read the "HEADER LINE" and discard it with fgets().

    The rest of the file could be read line by line with fgets(), then process each line with a simple sscanf() call.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scan for a specific data
    By Bryan_James in forum C Programming
    Replies: 1
    Last Post: 12-13-2014, 08:31 AM
  2. Replies: 4
    Last Post: 10-04-2013, 03:07 PM
  3. Read text file, scan and store data into variables?
    By wisdom30 in forum C Programming
    Replies: 8
    Last Post: 04-18-2011, 11:23 PM
  4. Scan data from file
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-12-2002, 07:52 AM
  5. Retrieve specific data from text for edit
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 01-22-2002, 09:02 AM

Tags for this Thread