In c programming Part A: Writing into a Sequential File Write a C program called
ID: 3919807 • Letter: I
Question
In c programming
Part A: Writing into a Sequential File Write a C program called "Lab5A.c" to prompt the user and store 5 student records into a file called "stdInfo.txt". This "stdInfo.txt" file will also be used in the second part of this laboratory exercise 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 input, printing and saving the records to the file # include struct student f char firstname [40] char lastname [40]; int id; float GPA; int year; typedef struct student Student; /* Input the student data interactively from the keyboard */ void InputstdRecord (Student *stdList); /* Display the contents of Student records from the list */ void PrintStdList (const Student *StdList); /* Save the student records from the list to the newly created text file specified by FileName */ void SaveStdList (const Student *StdList, const char *FileName); int main() i Student StdList[5] InputStdRecord(StdList); PrintstdList (StdList); SaveStdList (StdList, "stdInfo.txt"); return 0;Explanation / Answer
If you have any doubts, please give me comment...
Lab5A.c
#include<stdio.h>
struct student{
char firstname[40];
char lastname[40];
int id;
float GPA;
int year;
};
typedef struct student Student;
void InputStdRecord(Student *StdList);
void PrintStdList(const Student *StdList);
void SaveStdList(const Student *StdList, const char *FileName);
int main(){
Student StdList[5];
InputStdRecord(StdList);
PrintStdList(StdList);
SaveStdList(StdList, "stdInfo.txt");
return 0;
}
void InputStdRecord(Student *StdList){
int i=0;
for(i=0; i<4; i++){
printf(" Enter Student %d details: ", i+1);
printf("ID: ");
scanf("%d", &StdList[i].id);
printf("Firstname: ");
scanf("%s", StdList[i].firstname);
printf("Lastname: ");
scanf("%s", StdList[i].lastname);
printf("GPA: ");
scanf("%f", &StdList[i].GPA);
printf("Year: ");
scanf("%d", &StdList[i].year);
}
}
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);
}
Lab5B.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 wordCap(char *word);
void SaveStdList(const Student *StdList, const char *FileName);
int main(){
Student StdList[5];
ReadStdRecords(StdList, "stdInfo.txt");
PrintStdList(StdList);
SaveStdList(StdList, "stdInfo.txt");
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);
wordCap(StdList[i].firstname);
wordCap(StdList[i].lastname);
}
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 wordCap(char *word){
int i=0;
while(word[i]!=''){
if(word[i]>='a' && word[i]<='z')
word[i] = word[i]-32;
i++;
}
}
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.