Hey there guys,
I have written a program to open a file, read from it and store the data into a structure. The program works fine except when i try to use an empty file to read from the program stops responding, and after debugging it, DEV c++ says that an access violation segmentation fault has been raised in the program. Im not really sure where to start, whether its to do with the opening of the file or whether is is to do with reading in the data. Any help would be great. here is the data for the test file if anyone wants to see how it works.
----------------------------------------------------
1 25 1 Sisters Anniversary
0 5 7 Mums Birthday
1 25 2
0 16 12 My Birthday
1 6 7 Nans and Grandads Anniversary
---------------------------------------------------
Code:
// included libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// constants
const char *MONTHS[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
//array of months
const int ARRAY_LENGTH = 5;// array length
const int DESC_LENGTH=256;
//------------------------------------------------------------------------------
//structures--------------------------------------------------------------------
struct EventDate {
int day;
int month;
};
enum EventType {Birthday, Anniversary};
struct Event {
EventType type;
EventDate date;
char description[DESC_LENGTH];
};
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Function prototypes
FILE *safeOpen(char *fileName, char *mode);
// Purpose: To safely open a file
// Arguments: the file name of the file to be opened and the mode
// Ret. Val: a pointer to the input stream
// Pre: file to be opened must exist
// Post:
//------------------------------------------------------------------------------
Event inputEvent(FILE *inputStream);
// Purpose: To input a single event from the file
// Arguments: the pointer to the input stream
// Ret. Val: an event structure
// Pre: array to store the structure must be declared
// Post:
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//main
int main(int argc, char *argv[]) {
FILE *inputStream; // creates file pointer
Event events[ARRAY_LENGTH]; //array to store events
int arrayIndex = 0; // index for array of dates
Event tempEvent;// to store event temporarily for swap
int i,j; // counters
//check for correct number of args
if (argc < 2 ) {
printf("USAGE: %s <file to get events from> \n", argv[0]);
exit(1);
}
inputStream = safeOpen(argv[1], "r");// assign file to inputstream
// store events into array until EOF is reached
while (!feof(inputStream)) {
events[arrayIndex] = inputEvent(inputStream);
arrayIndex++;
fscanf(inputStream, " ");
}// end of while loop
// bubble sort algorithm
for (i=0; i < ARRAY_LENGTH; i++) {
for (j=0; j < ARRAY_LENGTH; j++) {
if (((events[j].date.month) > (events[j+1].date.month)) ||
((events[j].date.month) == (events[j+1].date.month)) &&
((events[j].date.day) > (events[j+1].date.day))) {
tempEvent = events[j];
events[j] = events[j+1];
events[j+1] = tempEvent;
} // end of if
} //end of inner j for loop
} // end of outer i loop
// output the array of structures
for (i=0; i < ARRAY_LENGTH; i++) {
if (events[i].type == Anniversary) {
printf("Anniversary: ");
}
else if (events[i].type == Birthday) {
printf("Birthday: ");
}
printf("%i ", events[i].date.day);
printf("%s\n", MONTHS[events[i].date.month-1]);
printf(" - %s\n\n", events[i].description);
}// end of for loop
// Close the file
fclose(inputStream);
}
//------------------------------------------------------------------------------
// function to safely open a file and check for success
FILE *safeOpen(char *fileName, char *mode){
FILE *stream; // creates file pointer
stream = fopen(fileName, "r"); // attach file to stream
// check for open file success
if (stream == NULL){
printf("Error test opening file");
exit(2);
}
return stream; // return inputStream pointer
}// end of function
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//function to input data from file into structure
Event inputEvent(FILE *inputStream) {
Event tempEvent;// variable to store the data from file
int type=0;// variable to store the event type
fscanf(inputStream,"%i ", &type);
// switch to set the enumerated type
switch (type == 0) {
case true:
tempEvent.type = Birthday;
break;
case false:
tempEvent.type = Anniversary;
break;
} // end of switch
fscanf(inputStream, "%i ", &tempEvent.date.day);
fscanf(inputStream, "%i ", &tempEvent.date.month);
fscanf(inputStream, "%[^\n]", tempEvent.description);
return tempEvent;// return structure
}// end of function