#include #include #include #include typedef struct Student { int age; double gpa; char *name; } Student; void printStudent( FILE *fp, const Student *s ) { fprintf( fp, "%-12s age=%3d gpa=%4.2lf\n", s->name, s->age, s->gpa ); } void readStudent( FILE *fp, Student *s ) { static char nameBuffer[ 1024 ]; fscanf( fp, "%s %d %lf", nameBuffer, &s->age, &s->gpa ); s->name = strdup( nameBuffer ); } int main( ) { Student s[ 2 ]; int i; FILE *output; FILE *input; input = fopen( "input.txt", "r" ); output = fopen( "output.txt", "w" ); if( input == NULL || output == NULL ) { fprintf( stderr, "Error opening file\n" ); return 1; } for( i = 0; i < 2; i++ ) { readStudent( input, &s[ i ] ); } for( i = 0; i < 2; i++ ) printStudent( output, &s[ i ] ); fclose( output ); return 0; }