In C, I need to read from a file.txt and need to combine the 2 struct to print out a the teacher's name from the teacher

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

In C, I need to read from a file.txt and need to combine the 2 struct to print out a the teacher's name from the teacher

Post by answerhappygod »

In C, I need to read from a file.txt and need to combine the 2
struct to print out a the teacher's name from the teacher struct,
and the student's name, and ID from the students struct. You
should have an input file, in this case using argv, which I already
implemented, It should read the student.txt and
teacher.txt files, and combine an output to look like this:
----------OUTPUT----------
Student: Joseph ID: 156746 Teacher Name: Phillips
Student: Roger ID: 454987 Teacher Name: Phillips
Student: Jessica ID: 449624 Teacher Name: Steven
------------------------------------------------------------------------
#include
#include
#include
#define MAX_STUDENTS 20
struct student
{
char name[20];
int age;
int id;
char state[50];
}student[MAX_STUDENTS];
struct teachers
{
char name[20];
int grade;
}
int main(int argc, char *argv[])
{
FILE *file;
char filename[50];
char buf[500];
if(argc > 2)
{
printf("Too many argument\n");
}
strcat(filename, argv[1]);
file = fopen(argv[1], "r");
if(file == NULL)
{
printf("Cannot open file\n");
return 1;
}
students.txt
Joseph 25 156746 California
Roger 20 454987 Oregon
Jessica 19 449624 California
teacher.txt
Steven Junior
Phillips Freshman
Laura Senior
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply