get line of code from text file

Jul 26, 2008 at 2:23pm
Hi,
im writing a program in c++ where their prompted to enter in a file name and i will display the info in the text file in a fashionable way the text file looks like
55 5 5 54
55448
54854
56985
how do i only grab certian lines. i cant figure it out for the life of me, is it cin.getline or ??? any help would be greatly appreciated
i would like to make output look like
line 2 says ....
line 4 says ....
line 3 says ....
line 1 says .....
Jul 26, 2008 at 4:07pm
Use the std::getline() function defined in <string>:
1
2
string s;
getline( cin, s );

Hope this helps.
Jul 26, 2008 at 5:54pm
so how exactly would i word that so that it calls for a certian line in the text file???
string s;
getline( cin, s);
where s is say line 2??
Jul 26, 2008 at 6:34pm
If you know that you can read exactly one line at a time... then how would you read line 2?

If you want to remember more than one of the lines you've read, you can save them in a std::vector, then index the vector to access a specific line.
1
2
3
4
5
6
7
string s;
vector <string> v;
while (getline( myfile, s ))
  {
  v.push_back( s );
  }
cout << "line 2 = " << v[ 2 ] << endl;


Good luck!
Topic archived. No new replies allowed.