I have the following class
class Film {
Person authors[5]; //This will actually include only the director
string title;
string producer;
int n_authors;
int year;
int running_time;
Person actors[5];
int n_actors;
}
And the following file format (don’t ask me why I use this, I MUST use this format)
Stanley
Kubrick
#
2001: A Space Odissey
*
1968
161
Keir
Dullea
Gary
Lockwood
#
The # indicates the end of a list (in this case a ‘Person’ class), while the * a missing field (in this case the producer, btw the producer field must be filled with an * in the class).
The class Person consists of Name and Surname and has an overloaded operator >> that calls:
void load(ifstream& in) {
getline(in,name);
getline(in,surname);
}
What’s the best method to parse this file structure? I can’t use regular expressions or anything more advanced than ifstream. My concern is on how (and where in the code) to detect the end of file and the end of a list of people.
The standard line reading idiom:
Where it says “process line” you should add some logic that tracks the current state of the parser.
For your simple application you could proceed in bits, reading lists and tokens as specified by the format. For instance:
Now you can say:
You can use
std::stoito convert tokens 3 – 5 to integers:Note that the
n_authorsandn_actorsvariables are redundant, since you have self-terminated lists already. You can or should use the variables as an integrity check if you like.