Program openes input file and prints current reading/writing position several times.
If file is formated with '\n' for newline, values are as expected: 0, 1, 2, 3.
On the other side, if the newline is '\r\n' it appears that after some reading, current position returned by all tellg() calls are offsetted by the number of newlines in the file - output is: 0, 5, 6, 7.
All returned values are increased by 4, which is a number of newlines in example input file.
#include <fstream>
#include <iostream>
#include <iomanip>
using std::cout;
using std::setw;
using std::endl;
int main()
{
std::fstream ioff("su9.txt");
if(!ioff) return -1;
int c = 0;
cout << setw(30) << std::left << " Before any operation " << ioff.tellg() << endl;
c = ioff.get();
cout << setw(30) << std::left << " After first 'get' " << ioff.tellg() << " Character read: " << (char)c << endl;
c = ioff.get();
cout << setw(30) << std::left << " After second 'get' " << ioff.tellg() << " Character read: " << (char)c << endl;
c = ioff.get();
cout << setw(30) << std::left << " Third 'get' " << ioff.tellg() << "\t\tCharacter read: " << (char)c << endl;
return 0;
}
Input file is 5 lines long (has 4 newlines), with a content:
-------------------------------------------
abcd
efgh
ijkl
--------------------------------------------
output (\n):
Before any operation 0
After first 'get' 1 Character read: a
After second 'get' 2 Character read: b
Third 'get' 3 Character read: c
output (\r\n):
Before any operation 0
After first 'get' 5 Character read: a
After second 'get' 6 Character read: b
Third 'get' 7 Character read: c
Notice that character values are read corectly.