Write/Read text file

Hi, I want to create a program which let me write to text file and read from this text file. I have problem with second part of this exercise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 #include <stdlib.h>
#include <stdio.h>

int main()
{

 char name[50];
 
 int weight,height,age;
 char tab[100];
 ;
 int i;

 FILE *file;

     file = fopen("student.txt","w");

     
     scanf("%s",&name);
    
     scanf("%d",&weight);
    
     scanf("%d",&height);
     
     scanf("%d",&age);


     fprintf(file,";%s %s %d %d %d",name,weight,height,age);
     fclose(file);

     file = fopen("student.txt","r");

     while(tab!=EOF)
     {

      fgets(file,"%s",tab);
      printf("%s",tab);
     }



     fclose(file);


    return 0;
}
Replace this
> while(tab!=EOF)
with this
1
2
3
while ( fgets(tab,sizeof(tab),file) ) {
    printf("%s",tab);
}
Thanks a lot, still I have problem with understanding how compilator recognize end of file in while loop.
http://man7.org/linux/man-pages/man3/fgets.3p.html

Because
while ( fgets(tab,sizeof(tab),file) )

is like
while ( fgets(tab,sizeof(tab),file) != NULL )

As the man page says, at end of file, fgets() returns NULL.

So this while loop loops until end of file.
Topic archived. No new replies allowed.