Reading a File and showing on Screen using getline : C++ -


i trying read file below content:

this line 1 line 2 line 3 line 4 line 5 

code:

#include <iostream> #include <fstream> #include <limits>  using namespace std;  int main() {      char buff[50];      ifstream is("test.txt");     if(!is)     cout << "unable open test.txt\n";      while(is.getline(buff,50,'\n'))     {         cout << is.gcount();         cout << buff;         cout << "\n----------------\n";         }     return 0;     } 

the output:

$ ./trial 18this line 1 ---------------- 18this line 2 ---------------- 20this line 3 ---------------- 19this line 4 ---------------- 17this line 5 ---------------- 

now suppose if comment cout << "\n----------------\n"; i.e.

  while(is.getline(buff,50,'\n'))     {         cout << is.gcount();         cout << buff;         //cout << "\n----------------\n";         } 

i output as:

$ ./trial 17this line fivee 

i not able figure out - why such behavior ?

also why showing count 18 (suppose first line - first line contains 16 characters including white spaces - if add null becomes 17 - endof line character discarded getline).

i using windows-7 , cygwin.

you state:

i using windows-7 , cygwin.

the problem cygwin environment reading file in binary mode, file saved in dos text format. means each line emitted, trailing \r character causes next emitted line overwrite previous one.

the 18 output vs. 16 expected because of \r plus trailing \n.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -