Originally posted by Aakash Datt
How do I do that?
First, look up std::getline(). It takes three parameters: the input stream, the destination (using a string would be your best bet), and the delimeter (optional, because it defaults to '\n').
For example:
Code:
ifstream in( "somefile.dat" );
string data;
std::getline( in, data, ',' );
// this reads the file up to the first comma, and puts it into "data"
Repeat this process for each of the pieces of data you need. Except, instead of declaring the string "data" as I did in the example above, it will already be declared as part of the structure you're using.
Code:
struct student {
string name;
int id_number;
// etc.
};
std::getline( in, name );
It does get slightly more complicated when you have a line like:
Science 89, 48
if the 89 is not part of the name of the course. This is because part of it is delimited by whitespace, and part by commas. First, I'd use the >> operator to read the name of the class (as long as its always 1 word). Then you could use getline to read until the next comma, but before that you would probably want to call cin.ignore() to skip the space. You store the "89" in a string (or char*) and use atoi() on it to convert it to a number before storing it in some_student.id_number.
If all this seems too complicated, it's really a bunch of nitpicky things. The problem is that the input file format isn't very regular, or easy to work with. Good luck, and I hope this helps.