Storing Strings Read From a File

I am trying to store an nfl schedule from a .txt file, but I am having trouble in how to sift through the file and store it appropriately.

The format of the file is:

week 4 : { ('TT','AF'), ('SS','AC'), ('CIN','PS'), ('NEP','BB'), ('CLV','BR'), ('OR','IC'),('TBB','LAR'), ('MV','CB'), ('CP','HT'), ('LAC','MD'), ('KCC','DL'), ('JJ','DB'), ('DC','NOS'), ('WR','NYG'), ('PE','GBP') } week 5 : { ('IC','KCC'), ('LAR','SS'), ('CLV','SF'), ('CB','OR'), ('AC','CIN'), ('NYJ','PE'), ('NEP','WR'), ('BR','PS'), ('GBP','DC'), ('JJ','CP'), ('MV','NYG'), ('DB','LAC'),('TBB','NOS'), ('AF','HT'), ('BB','TT') }

how would I go about storing the info between the () together, as well as the things between the {}?
Have you given an attempt to figure out out? We learn better when we try something and maybe fail.

Some hints (not sure how experience you are so):
- In C++ you use an std::ifstream object to read a file as a stream.

- e.g. if I have an object std::ifstream fin("file.txt");,
I can read in the first space-delimited token of that file by doing:
int my;
fin >> my_int;

or perhaps
std::string str;
fin >> str;

But also, std::getline() can be used to parse a stream by more than just whitespace, e.g. you could delimit each token by a comma:
std::string line;
std::getline(fin, line, ',');

In your case, a good start would be to simply read in each space-delimited token with the >> operator. Ignore the token of it's a "{", "}", ":", or "week", do something special if it's a number (e.g. 5), but then actually parse the string more on your own if the string looks like "('AA','BB'),".

Also, a bit more complicated, but a string can always be turned back into another stream by using an std::stringstream. You can search that if you want examples. This allows you to parse part of a line, which was already the output of a stream, as another stream.
Last edited on
Topic archived. No new replies allowed.