Okay. I have a file called "Graduates.txt" in my home directory.
I have a portable program to find the home directory, and I opened the file for reading.
Data in the file looks something like this:
year,firstName,lastName
I need to get this data from this file, and separate it into my struct:
typedef struct alumnus {
int yearGraduated;
char firstName[30];
char lastName[30];
} Alumns;
I have a thought that may or may not work:
A while loop reads through the file, using fgets() to get the data. It then copies it to the struct... but I don't know how to implement any of this.
Sorry if this sounds like dumb question, it most likely is.
Implemented using dasblinkenlight hint.
Use strtok() for the same. e.g
3.Remember strtok() is non-reentrant function,so store the results returned from evey function call of strtok();
Here is a quick example of reading input, using
fopen()
,fgets()
andstrtok()
and how to format output using correct format strings: (output shown here)Edited to show placing values into struct Alums