So this is my structs.h file:
Code:
#ifndef STRUCTS_H
#define STRUCTS_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct{
char AISID[10];
float latitude;
float longitude;
float direction;
float speed;
}shipInfo; /* Create a structure that will store all of the info for each individual ship */
typedef struct{
int day;
int month;
int year;
int hour;
int minute;
int second;
}dateTime; /* Create a structure that will store the date and time for the ships in a file
#ifdef __cplusplus
}
#endif
#endif /* STRUCTS_H */
and my main.c now looks like this:
Code:
int main(int argc, char** argv) {
char text[500]; /* Create a character array that will store all of the text in the file */
char line[100]; /* Create a character array to store each line individually */
FILE *file; /* Create a pointer to the file which will be loaded, to allow access to it */
char fileName[30]; /* Create a character array to store the name of the file the user want to load */
printf("Enter the name of the file containing ship information: ");
scanf("%s", fileName);
/*Try to open the file specified by the user. Use error handling if file cannot be found*/
file = fopen(fileName, "r"); /* Open the file specified by the user in 'read' mode*/
if(file == NULL){
perror("The following error occurred: ");
printf("Value of errno: %d\n", errno);
}
else {
printf("File loaded. "); /* Display a message to let the user know
* that the file has been loaded properly */
}
while(fgets(line, 100, file)!="\n"){
}
/*
fscanf(file, "%s", text); /* Read all the text from the file and store in the character array 'text'
printf(text); /* print out the contents of the 'text' array to show that the info is being saved there
*/
fclose(file);
return 0;
return (EXIT_SUCCESS);
}
If I'm right, I need to insert code into the while loop at the end of my main method, which will read a line of text, then use strtok to split the line up into tokens, and store each token into one of the variables in one of the structs.
Only the first line of data in the .txt file will be stored in the 'dateTime' struct, and each subsequent line will be stored in the 'shipInfo' struct.
An example of the first few lines in the .txt file:
13 11 2011 13 04 00
GW1927 52.408 -4.117 1.000 0.000
GS452 51.750 -4.300 5.000 10.000