Part C: Writing into a Direct Access File Write a C program called \"LabSC.c to
ID: 3919946 • Letter: P
Question
Part C: Writing into a Direct Access File Write a C program called "LabSC.c to read the student infomation 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): D 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 char firstnane[40) ehar lastname [401 int id float GPA int year typedef struct student Student Input the student data from the file specified by FileName void InputstdRecord (StudentSedList, const char ileNane): /Display the contents of Student recorda from the 1ist void PrintStdlist (conat Student Stdlist) Save the student records from the list to the newly ereated binary file specified by FileNane void SaveStdList(const Student Stdist, eonst char rileName) int main Student Stdlist[s) InputstdRecord (Sedlist, "stdInfo.txt") Printstdlist (Stdlist) SaveStdlist(Stdlist, "stdinfoDA. dat" return 0 Note that the function SaveStdListo must perform opening of the file stdInfoDA.dat for writing in binary mode, then writing all records, and finally closing the file. Make sure that the character arrays used for first and last name are fully initialized e.g. use vo) before writing to file (to ensure that all data fields hold initial data). Part D: Reading and modifying data from a Direct Access File Write a different C program called "Lab5D.c" that performs an in-place sort by GPA of the data in "stdInfoDA.dat. This means that the records in the file must be re-arranged, or sorted, using the GPA as the sorting key value. You may not use an array to hold all file data. You may only store records in at most two data structures within your program. The records must be in order from highest to lowest GPA. To prove if your program works start by reading in each record and then outputting the information to the monitor (stdout) until encountering end-of-file; during this process you can determine the number of records in the file. After closing and re-opening the file, perform a sort in much the same way that one sorts elements of an array-if two ad acent records are not in the correct order, then swap them. Use a temporary structure to swap records. You may use any algorithm for sorting. Finally, after the sort is finished, once again output all records to show that they appear in sorted orderExplanation / Answer
SOLUTION:
According to the given data the below code follows;
PART C:
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);
}
PART D:
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);
}
Therefore by using the above data we can get our required output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.