Hi I need to read a file that looks like this...
1|Toy Story (1995)|Animation|Children's|Comedy
3|Grumpier Old Men (1995)|Comedy|Romance
4|Waiting to Exhale (1995)|Comedy|Drama
5|Father of the Bride Part II (1995)|Comedy
8|Tom and Huck (1995)|Adventure|Children's
9|Sudden Death (1995)|Action
void readingenre(string filename,int **g)
cout << "reading file "+filename << endl;
//cout <<item<< "\t";
cout << "reading genre file finished" <<endl;
Grumpier Old Men (1995)
Attempting to parse this input file one field at a time is the wrong approach.
This is a text file. A text file consists of lines terminated by newline characters.
getline() by itself, is what you use to read a text file, with newline-terminated lines:
while (std::getline(myfile, line))
So now you have a loop that reads each line of text. A
std::istringstream can be constructed inside the loop, containing the line just read:
and then you can use
std::getline(), with this
std::istringstream with the optional delimiter character overriden to
'|' to read each field in the line.