-1

click here for the text file

I used the following code in Code::Blocks IDE. I get the number of lines as 2. Kindly help me with the code.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main()
{
ifstream in("readInt.txt", ios::in);
if(!in)
{
    cout << "Cannot open file";
    return 1;
}

string str;
int j=0;

while(in)
{
    getline(in,str);
    j++;
}

cout << "No of lines are: " << j;
in.close();
return 0;

}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Vishal
  • 11
  • 1
  • 1
    How many lines are in the file? Nevermind. I see there are 4 lines in the externally linked file. – wallyk Mar 03 '17 at 05:48
  • yes I'm getting output as 2. Is there a mistake in the code? – Vishal Mar 03 '17 at 05:52
  • Use `while (getline(in, str) ) { ... }`. – R Sahu Mar 03 '17 at 05:53
  • 1
    @RSahu: If anything, the `while(in)` should produce a *too high* count, not a too small count. – Cheers and hth. - Alf Mar 03 '17 at 06:00
  • Do you really mean for your loop to do things in the order "try to read -- increment the count -- check if the read was successful" ? –  Mar 03 '17 at 06:09
  • `while(!in.eof()){...}` Best to forget you ever thought of this option. It never works. [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Mar 03 '17 at 06:20

2 Answers2

2

You get a too small result because the line endings in your text file are encoded in a different way than the convention on your system.

Save or recreate the file with correct line endings for your system.


In the other direction, towards a too high result, the presented code

while(in)
{
    getline(in,str);
    j++;
}

… would produce a count of 1 for an empty file.

Instead do

while( getline(in,str) )
{
    j++;
}

Note: this remark only covers correctness, not efficiency.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • How can I save or recreate the file with correct line endings followed by my system? – Vishal Mar 03 '17 at 06:10
  • @Vishal: In general by using an editor. I used Notepad++. In its *Edit* menu there's an *EOL conversion* submenu. Which has tree items: *Windows (CR LF)*, and *Unix (LF)*, and *Macintosh (CR)*. The last one is for historic reasons I believe. I think modern Macs use Unix convention. – Cheers and hth. - Alf Mar 03 '17 at 06:12
  • Re: how to change line endings: the `unix2dos` and `dos2unix` commands were written for this. If you need to do it programmatically (i.e. without an external app), it's not that difficult to do it on your own but you'll need to open the file in raw (binary) mode and adjust accordingly. You'll need some kind of parameter as a hint, though, unless you want to literally guess (which, in this case, a well-written function can do with a *very* high probability of being right). – frasnian Mar 03 '17 at 06:38
2

First, your text file has not new character so in the text there is only one line

Change it and try in your code with

while(getline(in,str))
{
    j++;
}

in this way you avoid to count extra line

EmmanuelAC
  • 141
  • 3