schedule.txt Class Schedule Class Time Physics 15:30 Calculus 9:00 Biology 14:30
ID: 3853271 • Letter: S
Question
schedule.txt
Class Schedule
Class Time
Physics 15:30
Calculus 9:00
Biology 14:30
Chemistry 11:30
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
FILE *fp;
int th[5], tm[5];
char apm[5][20];
char temp[20], cl[20];
int index;
int hours, minutes;
fp = fopen(argv[1], "r"); // opens the file in readonly mode
if (fp == NULL)
{
printf ("Please provide a text file. ");
return 0;
}
fscanf(fp, "%s", temp); // class is consumed
fscanf(fp, "%s", temp); // schedule is consumed
fscanf(fp, "%s", temp); // class is consumed
fscanf(fp, "%s", temp); // time is consumed
for (int i = 0; i < 5; i++)
{
fscanf (fp, "%s %d:%d", temp, &hours, &minutes);
if (hours < 12)
{
th[i] = hours;
tm[i] = minutes;
strncpy(apm[i], "AM", 20);
}
else if (hours == 12)
{
th[i] = hours;
tm[i] = minutes;
strncpy(apm[i], "PM", 20);
}
else
{
th[i] = hours - 12;
tm[i] = minutes;
strncpy(apm[i], "PM", 20);
}
}
fclose(fp);
printf("What class do you want to search for? ");
scanf ("%s", cl);
if (strcmp(cl, "Physics") == 0)
{
index = 0;
}
else if (strcmp(cl, "Calculus") == 0)
{
index = 1;
}
else if (strcmp(cl, "Biology") == 0)
{
index = 2;
}
else if (strcmp(cl, "Study") == 0)
{
index = 3;
}
else if (strcmp(cl, "Chemistry") == 0)
{
index = 4;
}
// print the result
printf("%s is at %d:%d %s ", cl, th[index], tm[index], apm[index]);
return 0;
}
--------------------
OUTPUT:
What class do you want to search for? Physics
Physics is at 3:30 PM
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.