reading the contents a file seems pretty straightforword, i.e.:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
However, what if you wanted to place the contents of the file into a struct for further manipulation. For example if I know the file has a list of pre-sorted employee names and id numbers how would you read that data into the structure. So...it would be taking the raw data and reading into a struct something like:
Code:
struct EMPLOYEE {
char firstname[40];
char lastname[40];
char employee_number[10];
};
I was thinking of first counting the total number of lines in the file and then dividing by 3 to determine the total number of "employees" that are in the file. Then from there going down the list and plugging in each line to a structure that identifies an employee.
Does this sound like a valid approach? Also, any tips or links ot helpful tutorials on an effective way to go about assigning the data groups to their individual struct / reading from a text file into a structure?
Also, how can I tell when there is a new line when reading a file? Do you just look the ASCII value for a newline?
Thanks!