$25
Store data into a sequential file.
• Retrieve data from a sequential file.
Exercise 1
A university needs to maintain a text file to store and retrieve their student’s details such as name, class, year and total marks.
1. Write a C program to do the following;
a) Create a file pointer
FILE *fPtr;
b) Open the file to write data
fPtr = fopen ( “Student.dat”, “w”);
c) Input the data of 5 students from the keyboard and store in the file. Write the data
to the file using
fprintf(fPtr, “%s %c %d %d \n”, name, class, year, GPA);
d) Modify the program to open the “Student.dat” file you created above and print the data in a tabular form as shown below in the screen.
Name Class Year GPA
Nimal 1 1 3.7
Sunil 1 2 4.0
Kamal 3 3 2.7
Mihiri 2 1 1.7
Mala 2 4 2.1
Exercise 2
Write another program to read the “Studnet.dat” file and display name and GPA of the students in a given year. The user should be able to input the year from the keyboard.
1