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
Posted: Mon May 09, 2022 6:00 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 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
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