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.