Link to home
Start Free TrialLog in
Avatar of Troudeloup
Troudeloup

asked on

getline

I have a txt file with 4 lines


line1
line2
line3
line4



I want to use a loop to store each line into an element in an array

string line[10];
int n = 0;
while ( n < 5 )
{
  //  getline
  n = n + 1;
}

return 0;



what is the syntax to read whole N th line in the file?
SOLUTION
Avatar of lucky_james
lucky_james
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Troudeloup
Troudeloup

ASKER

i should have said this:

I want to read from a txt file.
SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
while (getline(infile, line)) {
    lines.push_back(line);
}




i don't get this part.



ok how about, can you show me how to get the N th line and ignore the loop for now?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#include <iostream>
#include <windows.h>


using namespace std;




int main ()
{

      string line;
      ifstream filePtr("lines.txt");
      getline(filePtr, line);
      cout << line << endl;
      
      
      return 0;
}



i got this error message

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ah duh  >_<