-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_schedule_filewriter.c
73 lines (56 loc) · 2.77 KB
/
class_schedule_filewriter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* --------------------------------
-- Assignment Description --
Write a program that collects university class schedules from the user and writes them to a file named "schedule.txt".
Each entry in the schedule should include the class name, days of the week the class meets, the start time, the end time, and the number of credits for the class.
------------------------------------- />
#include <stdio.h>
#include <string.h>
int main(){
FILE *cfPtr = NULL; //file pointer
char className[40]; // class name
char daysOfWeek[7]; // days of the week
char startTime[8]; // store start time as char array
char endTime[8]; // store end time as char array
int numOfCredits; // number of credits for class
//open file if exists, exit program if not
if ((cfPtr = fopen("schedule.txt", "w")) == NULL) {
puts("File could not be opened");
} else {
puts("Enter EOF to end input.");
printf("%s", "Example: Ctrl + D");
printf("\nPress enter to begin");
// print each class to file as new line
while (fgets(className, sizeof(className), stdin) != NULL) {
// If user enters EOF, break out of the loop
if (strcmp(className, "EOF") == 0) {
break;
}
// get the classname
printf("\nEnter the class name.");
fgets(className, sizeof(className), stdin);
className[strlen(className) - 1] = '\0'; // Remove newline character
// get the days of the week
printf("\nEnter the letters for days of the week.");
fgets(daysOfWeek, sizeof(daysOfWeek), stdin);
daysOfWeek[strlen(daysOfWeek) - 1] = '\0'; // Remove newline character
// get the start time
printf("Enter the start time (format: HH:MMAM): ");
fgets(startTime, sizeof(startTime), stdin);
startTime[strlen(startTime) - 1] = '\0'; // Remove newline character
// get the end time
printf("Enter the end time (format: HH:MMAM): ");
scanf("%s", endTime); // Read until whitespace
endTime[strlen(endTime) - 1] = '\0'; // Remove newline character
// get the number of credits
printf("Enter the number of credits: ");
scanf("%d", &numOfCredits);
getchar(); // Consume newline character from input buffer
// Write to file
fprintf(cfPtr, "%s %s %8s %8s %d\n", className, daysOfWeek, startTime, endTime, numOfCredits);
// prompt for next class or to end
puts("Type enter to add a class");
puts("Enter EOF to end input.");
}
fclose(cfPtr); //close the file
}
}