Hello,
There is something wrong with my code, I can't seem to find my error. The program works just fine, there is something wrong in the function 'read_from_file'
Code:
#include<stdio.h>
struct student
{
int roll_no;
char name[ 50 ];
float CGPA;
};
void write_to_file( struct student [] );
void read_from_file( struct student [] );
int main()
{
struct student s1[ 5 ];
write_to_file( s1 );
printf( "\n\n" );
read_from_file( s1 );
}
void write_to_file( struct student s1[] )
{
int i = 0;
FILE *Ptr;
Ptr = fopen( "Students.txt", "w" );
fprintf( Ptr, "%-14s%-11s%5s\n", "Roll No.:", "Name:", "CGPA:" );
printf( "Student %0d: ", ( i + 1 ) );
scanf( "%d%s%f", &s1[ i ].roll_no, s1[ i ].name, &s1[ i ].CGPA );
while( !feof( stdin ) )
{
fprintf( Ptr, "%-14d%-10s%4.1f\n", s1[ i ].roll_no, s1[ i ].name, s1[ i ].CGPA );
i++;
printf( "Student %0d: ", ( i + 1 ) );
scanf( "%d%s%f", &s1[ i ].roll_no, s1[ i ].name, &s1[ i ].CGPA );
}
fclose( Ptr );
printf( "\n-> Writing on file \"Students.txt\"." );
}
void read_from_file( struct student s1[] )
{
int i = 0;
FILE *Ptr;
Ptr = fopen( "Students.txt", "r" );
printf( "\n-> Reading from file \"Students.txt\" and displaying on screen:\n\n" );
printf( "%-14s%-11s%5s\n", "Roll No.:", "Name:", "CGPA:" );
fscanf( Ptr, "%d%s%f", &s1[ i ].roll_no, s1[ i ].name, &s1[ i ].CGPA );
while( !feof( Ptr ) )
{
printf( "%-14d%-10s%4.1f\n", s1[ i ].roll_no, s1[ i ].name, s1[ i ].CGPA );
i++;
fscanf( Ptr, "%d%s%f", &s1[ i ].roll_no, s1[ i ].name, &s1[ i ].CGPA );
}
fclose( Ptr );
}
sorry for the long program, please help me