I am trying to read a file with the content below:
This is Line One This is Line Two This is Line Three This is Line Four This is Line Five
code:
#include <iostream> #include <fstream> #include <limits> using namespace std; int main() { char buff[50]; ifstream is("Test.txt"); if(!is) cout << "Unable to open Test.txt\n"; while(is.getline(buff,50,'\n')) { cout << is.gcount(); cout << buff; cout << "\n----------------\n"; } return 0; }
Output:
$ ./Trial 18This is Line One ---------------- 18This is Line Two ---------------- 20This is Line Three ---------------- 19This is Line Four ---------------- 17This is Line Five ----------------
Suppose now if I comment on cout << "\n----------------\n"; ie
while(is.getline(buff,50,'\n')) { cout << is.gcount(); cout << buff; //cout << "\n----------------\n"; }
I get output like:
$ ./Trial 17This is Line Fivee
I canβt understand - why such behavior?
Also why is it shown that the counter is 18 (suppose the first line - where the first line contains 16 characters, including spaces - if I add null, it becomes 17 - the end of line character is discarded by getline).
I am using windows-7 and cygwin.
source share