Question
I have this program that needs to be C89 compliant that reads a file from command line (Ex: ./a.out filename.csv). Here is my code but
I have this program that needs to be C89 compliant that reads a file from command line (Ex: ./a.out filename.csv). Here is my code but it has 3 errors that I would also liked fixed. I need help with the program to give the expected output (attached).
Code: ************************************************
#include
#include
#include
struct node {
char startTime [10];
char endTime [10];
char days [15];
int classCount;
};
int read (struct node array[], char* filename);
char* getToken(char buffer[], int position);
char* getString(char buffer[], int position);
int findClass(struct node array[], char* start, char* end, char* days, int size);
void printArray(struct node array[], int size);
void sortArray (struct node array[], int size);
int main (int argc, char* argv[])
{
struct node array [100];
int count;
if (argc !=2){
printf("usage: executable filename ");
return -1;
}
count = read(array, argv[1]);
sortArray(array, count);
printArray(array, count);
return 0;
}
int read (struct node array[], char* filename)
{
char buffer [100];
char temp [100];
char *token;
char *start, *end, *days;
int count =0, index;
FILE* fp;
if((fp=fopen(filename, "r"))==NULL){
printf("Unable to open file. ");
return -1;
}
/*column heading*/
fgets(buffer, sizeof(buffer), fp);
while(fgets(buffer, sizeof(buffer), fp) != NULL){
strcpy(temp, buffer); /*clean copy*/
/*gets the start time*/
start = getToken(temp, 11); /*this maybe 10? */
printf("%s--", start);
/*gets the end time*/
strcpy(temp, buffer);
end = getToken(temp, 12);
printf("%s", end);
/*gets the weekday schedule*/
strcpy(temp, buffer);
days = getString(temp, 13);
if((index = findClass( array, start, end, days, count)) == -1)
{
/*unseen class time/days*/
array[count].startTime = start;
array[count].endTime = end;
array[count].days = days;
array[count].classCount = 1;
count++;
}
else{
array[index].classCount +=1;
}
} fclose(fp);
return count;
}
char* getToken(char buffer[], int pos){
int i, tracker;
char days;
char *del =", ";
char *token = strtok(buffer, del);
for(i = 1; i token = strtok(NULL, del); return token; } char* getString(char buffer[], int pos){ int i; char *days; char *del =", "; char *token = strtok(buffer, del); for(i=1; i token = strtok(NULL, del); } if (strcmp(token,"Y")== 0){ days = "Mo"; } token = strtok(NULL,del); if (strcmp(token,"Y")== 0){ days = strcat(days, "Tu"); } token = strtok(NULL,del); if (strcmp(token,"Y")== 0){ days = strcat(days,"We"); } token = strtok(NULL,del); if (strcmp(token,"Y")== 0){ days = strcat(days, "Th"); } token = strtok(NULL,del); if (strcmp(token,"Y")== 0){ days = strcat(days,"Fr"); } token = strtok(NULL,del); if (strcmp(token,"Y")== 0){ days = strcat(days,"Sa"); } token = strtok(NULL,del); if (strcmp(token,"Y")== 0){ days = strcat(days,"Su"); } token = strtok(NULL,del); return days; } int findClass(struct node array[], char* start, char* end, char* days, int size){ int i; for(i=0; i if(array[i].startTime== start && array[i].endTime==end && array[i].days == days) return i; return -1; } void sortArray(struct node array[], int size){ int i, sort; struct node temp; do{ sort = 1; for(i=0; i if(array[i].classCount > array[i+1].classCount){ temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; sort = 0; } /*NEED TO DO STRCMP here because start time is a string*/ else if (array[i].classCount == array[i+1].classCount){ if(array[i].startTime temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; sort = 0; } } }while( !sort); } void printArray(struct node array[], int size){ int i; int classCount =0; for(i=0; i if(array[i].classCount == 1) printf("%s-- %s, %s, %d class ", array[i].startTime, array[i].endTime, array[i].days, array[i].classCount); else printf("%s-- %s, %s, %d class ", array[i].startTime, array[i].endTime, array[i].days, array[i].classCount); } } Partial OUTPUT: Partial File to read (Output and file are very long): Also here are the errors I am getting that I would like help with too, please:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started