Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with these 3 small C programs. Thank you. Structures: struct student_struct { char last_name[64]; char first_name[64]; char middle_names[64]; int id; char major[64];

Please help me with these 3 small C programs. Thank you.

image text in transcribed

image text in transcribed

Structures:

struct student_struct { char last_name[64]; char first_name[64]; char middle_names[64]; int id; char major[64]; }; 
struct course_struct { char code[10]; char term[6]; char title[128]; char instructor[64]; }; 
struct student_course_struct { int student_id; char course_code[10]; char term[6]; char registration[4]; int grade; }; 

Just declare students.bin as fp = ("students.bin", "w"); I don't think you need to actually see the contents of the .bin file. It is just a binary file that was opened in the first and previous question:

#include

#include "structs.h"

int main(void) {

//student_struct to store the student data

struct student_struct student_data;

//variable to keep track of maximum id in the file

int max_student_id = 0;

//variable for prompt to keep going or stop

char keep_going;

//file pointer to open "students.bin" file

FILE *students;

//open "students.bin" file in append binary mode

students = fopen("students.bin", "a+b");

//if uable to open "students.bin" file show the error message and

//exit the program

if(!students) {

printf("Failed to open \"students.bin\". ");

return -1;

}

//move the file pointer to end

fseek(students, 0, SEEK_END);

//ftell() tells the current position of the students pointer in the file

//if it is zero then file must ne empty. Hence no records, therefore set

//max_student_id to 0 as there is no previous record. Note we are setting

//max id to 0 here which will be incremented at insert time before insertion

//to file to making it 1

if(ftell(students) == 0) {

printf("In emoty file ");

max_student_id = 0;

} else {

//set the pointer again to begining of file

fseek(students,0, SEEK_SET);

//read the whole file to get the maximum student id in the file

while(fread(&student_data, sizeof(struct student_struct), 1, students)) {

if(student_data.id > max_student_id) max_student_id = student_data.id;

}//after this loop file ptr will be at end of the file since we are appending the content

//we will not reset it to start of file.

}

do {

//prompt for input data and input for student

printf("Last name: "); scanf("%s", student_data.last_name);

printf("First name: "); scanf("%s", student_data.first_name);

printf("Middle name: "); scanf("%s", student_data.middle_name);

printf("Major: "); scanf("%s", student_data.major);

//increment te max_student_id by 1 as it will become from 0 to 1 in case

//of empty file and 1 more than max id present in file before

max_student_id++;

//assign it to student_struct

student_data.id = max_student_id;

//write the stuct to the file and raise error if encountered

if(!(fwrite (&student_data, sizeof(struct student_struct), 1, students)))

printf("Error in writing records. ");

//clear the input buffer

while ((getchar()) != ' ');

//get the input 'y' or 'Y' to keep going and any other thing for aborting

printf("Do you want to enter more records? [y|Y]: "); keep_going = getchar();

} while(keep_going == 'y' || keep_going == 'Y');

//close the file

fclose(students);

return 0;

}

You will write a program called sort" which will sort the contents of the students in L e. The program wil operate by a loc n memor to store e t? contents or e le in an array of student struct. Then sort that array in order of student_id. Finally, (over-)write the contents of the file out to the "students.bin" file You will write a program called sort" which will sort the contents of the students in L e. The program wil operate by a loc n memor to store e t? contents or e le in an array of student struct. Then sort that array in order of student_id. Finally, (over-)write the contents of the file out to the "students.bin" file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

Students also viewed these Databases questions