Anon
Anon

Reputation: 357

How do I get the next line from a file

I am implemting a program using c++, and i have a problem of geting the next line from an input file. I used:

   const MAX 300;
   char oneline[MAX];
   ifstream in;
   in.open("input.txt);
   in.getline(oneline,MAX);

The function getline always gets me the first line in the file. The thing is, how can I get the next line in the file?

Upvotes: 1

Views: 20998

Answers (2)

badgerr
badgerr

Reputation: 7982

std::string line;    
while(in.good())
{
    getline(in, line);

    //do something with line
}

Since you're using C++ you should use std::string to read your lines.

Upvotes: 3

Ovilia
Ovilia

Reputation: 7310

while (getline(in,line,'\n')){
    //do something with line
}

Upvotes: 2

Related Questions