Part C: Writing into a Direct Access File Write a C program called \"Lab5C.c\" t
ID: 3919825 • Letter: P
Question
Part C: Writing into a Direct Access File Write a C program called "Lab5C.c" to read the student information stored in file "stdInfo.txt" and write them into a binary file called "stdInfoDA. dat" (DA is shorthand for Direct Access). This file will also be used in part D of this exercise. Note that the input can come directly from the file "stdInfo.txt" created in Part B. The format of the file would look like this sample (excluding the first line): ID FIRSTNAME LASTNAME GPA YEAR 10 JACK DRELL 64.5 2018 20 MINA ALAM 92.3 2016 40 ABED ALIE 54.0 2017 30 JIM SMITH 78.2 2018 The skeleton code is given below for your convenience. Use the 3 functions specified to perform the reading, printing and saving the records to the file. #include struct student ( char firstname [401 char lastname [401 int id; float GPA; int year; typedef struct student Student; / Input the student data from the file specified by FileName * void InputStdRecord (Student *StdList, const char *FileName)Explanation / Answer
If you have any doubts, please give me comment...
Lab5C.c
#include<stdio.h>
struct student{
char firstname[40];
char lastname[40];
int id;
float GPA;
int year;
};
typedef struct student Student;
void ReadStdRecords(Student *StdList, const char *FileName);
void PrintStdList(const Student *StdList);
void SaveStdList(const Student *StdList, const char *FileName);
int main(){
Student StdList[5];
ReadStdRecords(StdList, "stdInfo.txt");
PrintStdList(StdList);
return 0;
}
void ReadStdRecords(Student *StdList, const char *FileName){
FILE *fp;
fp = fopen(FileName, "r");
int i=0;
//for skipping first line
fscanf(fp, "%[^ ]s", StdList[0].firstname);
for(i=0; i<5; i++){
fscanf(fp, "%d %s %s %f %d", &StdList[i].id, StdList[i].firstname, StdList[i].lastname, &StdList[i].GPA, &StdList[i].year);
}
fclose(fp);
}
void PrintStdList(const Student *StdList){
printf(" ID FIRSTNAME LASTNAME GPA YEAR ");
int i;
for(i=0; i<4; i++){
printf("%d %s %s %.1f %d ", StdList[i].id, StdList[i].firstname, StdList[i].lastname, StdList[i].GPA, StdList[i].year);
}
}
void SaveStdList(const Student *StdList, const char *FileName){
FILE *fp;
fp = fopen(FileName, "w");
fprintf(fp, "ID FIRSTNAME LASTNAME GPA YEAR ");
int i;
for(i=0; i<4; i++){
fprintf(fp, "%d %s %s %.1f %d ", StdList[i].id, StdList[i].firstname, StdList[i].lastname, StdList[i].GPA, StdList[i].year);
}
fclose(fp);
}
Lab5D.c
#include<stdio.h>
struct student{
char firstname[40];
char lastname[40];
int id;
float GPA;
int year;
};
typedef struct student Student;
void ReadStdRecords(Student *StdList, const char *FileName);
void PrintStdList(const Student *StdList);
void sortStdListByGPA(Student *StdList);
void SaveStdList(const Student *StdList, const char *FileName);
int main(){
Student StdList[5];
ReadStdRecords(StdList, "stdInfoDA.dat");
printf("Before sorting... ");
PrintStdList(StdList);
sortStdListByGPA(StdList);
printf(" After Sorting... ");
SaveStdList(StdList, "stdInfo.txt");
PrintStdList(StdList);
return 0;
}
void ReadStdRecords(Student *StdList, const char *FileName){
FILE *fp;
fp = fopen(FileName, "r");
int i=0;
//for skipping first line
fscanf(fp, "%[^ ]s", StdList[0].firstname);
for(i=0; i<5; i++){
fscanf(fp, "%d %s %s %f %d", &StdList[i].id, StdList[i].firstname, StdList[i].lastname, &StdList[i].GPA, &StdList[i].year);
}
fclose(fp);
}
void PrintStdList(const Student *StdList){
printf(" ID FIRSTNAME LASTNAME GPA YEAR ");
int i;
for(i=0; i<4; i++){
printf("%d %s %s %.1f %d ", StdList[i].id, StdList[i].firstname, StdList[i].lastname, StdList[i].GPA, StdList[i].year);
}
}
void sortStdListByGPA(Student *StdList){
int i, j;
for(i=0; i<4; i++){
for(j=0; j<4-i-1; j++){
if(StdList[j].GPA < StdList[j+1].GPA){
Student temp = StdList[j];
StdList[j] = StdList[j+1];
StdList[j+1] = temp;
}
}
}
}
void SaveStdList(const Student *StdList, const char *FileName){
FILE *fp;
fp = fopen(FileName, "w");
fprintf(fp, "ID FIRSTNAME LASTNAME GPA YEAR ");
int i;
for(i=0; i<4; i++){
fprintf(fp, "%d %s %s %.1f %d ", StdList[i].id, StdList[i].firstname, StdList[i].lastname, StdList[i].GPA, StdList[i].year);
}
fclose(fp);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.