Better way for me to parse file

I am trying to parse a file that is in this format. Lets say I get this file as an example

gameDesign java cs3
java cs1
cSharp cs1
cs1 cs0
discreteMath
dataBases
cs2 cs1
advancedGraphics
cs3 cs2
operatingSystems
graphics cs3 operatingSystems
cs0 mathForKids
cs4 cs3 dataBases operatingSystems
bioinformatics discreteMath cs2
assembly cs1
mathForKids


I want to take in the line and I can take them in one word at a time. I know that I can do this but I am not sure how to get it to take it in this manner.

I want the first line to be gameDesign java cs3 then the second line to be java cs1 but I get java cs1 cSharp and I know why cause it keeps getting more elements.

I have this code so far
1
2
3
4
5
6
7
8
		ifstream myfile;
		myfile.open ("input.txt");
		while(!myfile.eof()) // To get you all the lines.
		{
			myfile >> name >> req1 >> req2;
			cout<<name<<" "<<req1<<" "<<req2<<endl;
		}
		myfile.close();


How can I fix this?
It seems it's time to use vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ifstream myfile("input.txt");
vector<string> v_s;
while(myfile.good())
{
    string str;
    myfile >> str;
    v_s.push_back(str);
}
myfile.close();

for(int i = 0; i<v_s.size();i++)
{
    cout << v_s.at(i);
}
Last edited on
Use std::getline to read each line separately. http://www.cplusplus.com/reference/string/string/getline/

Spoiler: http://ideone.com/m2rxtU
Topic archived. No new replies allowed.