Data file in text format even though the actualy file is binary.
Code:
Math 102 10 M 0800 0850 Schulte
Eng 033 1 T 0930 1050 Shakespeare
Art 308 2 M 0800 1150 VanGogh
Anth 055 13 T 1200 1325 Kroeber
CS 125 2 T 0800 0850 Hoare
Eng 202 10 M 1000 1050 Chaucer
Chem 100 5 T 1100 1250 Pauling
Phys 395 2 T 1200 1250 Einstein
CS 125 4 M 1030 1120 Knuth
Math 420 2 T 0800 0950 al-Khowarizmi
main.c
Code:
#include "classes.h"
int main(void)
{
int switchInput;
int i = 0;
int tempCourse = 0;
char caseOneDept[5];
char caseTwoDept[5];
int caseTwoCourse = 0 ;
FILE *filePointer;
sched_record data[MAX_RECORD];
filePointer = fopen (BINFILE, "rb");
if (filePointer == NULL) {
printf("**Can't open file**");
printf("**Check file permissions of file path**\n");
exit(1);
}
printf("Enter your choice: ");
scanf("%d", &switchInput);
switch(switchInput)
{
case 1:
fread(data, sizeof(sched_record), MAX_RECORD, filePointer);
fclose(filePointer);
printf("Enter Dept Name: ");
scanf("%s", caseOneDept);
for (i=0; i<MAX_RECORD; i++){
if (strcmp(caseOneDept, data[i].Dept)==0){
if(tempCourse != data[i].course){
printf("\n%s %d", data[i].Dept, data[i].course);
tempCourse = data[i].course;
}
}
}
break;
case 2:
fread(data, sizeof(sched_record), MAX_RECORD, filePointer);
fclose(filePointer);
printf("Enter Dept Name: ");
scanf("%s", caseTwoDept);
printf("Enter Course Number: ");
scanf("%d", &caseTwoCourse);
qsort(data, MAX_RECORD, sizeof(sched_record), sortFunction);
for (i=0; i < MAX_RECORD; i++){
if ((strcmp(caseTwoDept, data[i].Dept)==0) && caseTwoCourse == data[i].course){
printf("\n%d%s", data[i].start.hour, data[i].Dept);
}
}
break;
}
return 0;
}
classes.h
Code:
#ifndef CLASSES_H_INCLUDED
#define CLASSES_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BINFILE "classes.db"
#define MAX_RECORD 10
typedef enum {MW, TR} days;
typedef struct {
int hour, min;
} Time;
typedef struct {
char Dept[5];
int course, sect;
days meet_days;
Time start, end;
char instr[20];
} sched_record;
int sortFunction(const void *p, const void *q);
#endif // CLASSES_H_INCLUDED
sortFunction.c
Code:
#include "classes.h"
int sortFunction(const void *p, const void *q) {
return ((sched_record *) p)->start.hour -
((sched_record *) q)->start.hour;
}
I can verify the Departments and courses are there.
Should not variable names represent what they are, not criptic names no one can understand when reading code?