How to recognize i there is a new line in file

HI!
I have a file and I want to attempt to know where in file there is a "\n" character.
suppose my "info.txt" looks like this :
1
2
3
4
5
6
7
 hi hello
dog cat
bye goodbye

hello
cat
goodbye


I want to know when i reach the part 2 of the file?
THNX!
There are lots of newlines in the file. Each line ends with a newline character. The blank line is a place where there are two consecutive newline characters.

But still, I'd look a the question differently. Use getline() to read a line at a time. The blank line means the length of the line is zero.

http://www.cplusplus.com/reference/string/string/getline/
http://www.cplusplus.com/reference/string/string/size/
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main ()
{
  string mystr;
  ifstream fileobj ("file.txt");

  if (fileobj.is_open()) {
    while ( getline(fileobj,mystr) ) {
        cout<<"mystr= "<<mystr<<endl;
        if (mystr=="") {
            cout<<endl<<"Hey! This is the empty line!"<<endl;
            break;
        }
    }
  }
  return 0;
}
mystr=  hi hello
mystr= dog cat
mystr= bye goodbye
mystr= 

Hey! This is the empty line!
Topic archived. No new replies allowed.