"get pointer" will be dirfted after invoking tellg()

IOFile.txt as follows:

abcd
efg
hi
j
kl
mno
pqrs
tuvwxyz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main()
{
    ifstream inFile("IOFile.txt",ifstream::in);
    char ch;

    inFile.get(ch);
    cout<<(int)ch<<" ";
    cout<<inFile.tellg();
    cout<<endl;

    inFile.get(ch);
    cout<<(int)ch;
    cout<<endl;

    inFile.get(ch);
    cout<<(int)ch;
    cout<<endl;

    inFile.get(ch);
    cout<<(int)ch;
    cout<<endl;

    inFile.get(ch);
    cout<<(int)ch;
    cout<<endl;

    inFile.get(ch);
    cout<<(int)ch;
    cout<<endl;

    return 0;
}


97 9
10
104
105
10
106

There is a strange problem. I invoke the inFile.tellg() in the seventh line. The code print the position of "get pointer", and then print the characters one by one.
In fact, the program can't print the characters correct. After invoking inFile.tellg(), the "get pointer" will be drift x bytes. I don't know why. And when I modify the IOFile.txt, the drift value x will change.


1. what's the meaning of tellg() return value?
2. why "get pointer" will be drifted after invoking tellg()?
I compile code using GCC 3.4.5 in windows7.
Thanks a lot



In reference to tellg()
Get position of the get pointer.
Returns the absolute position of the get pointer.

The get pointer determines the next location in the input sequence to be read by the next input operation.

It is a flaw in the design of C and C++ I/O.
Make sure you open in ios::binary mode. Then watch out for newlines.

It is a flaw in the design of C and C++ I/O.
Make sure you open in ios::binary mode. Then watch out for newlines.


Is it mean I can only use tellg() and seekg() when the open mode is binary?
I open the stream in ios::binary mode. Everything is OK.

IOFile.txt as follows:

abcd
efg
hi
j
kl
mno
pqrs
tuvwxyz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main()
{
    ifstream inFile("IOFile.txt",ifstream::in|ifstream::binary);
    char ch;

    inFile.get(ch);
    cout<<(int)ch<<" ";
    cout<<inFile.tellg();
    cout<<endl;

    inFile.get(ch);
    cout<<(int)ch;
    cout<<endl;

    inFile.get(ch);
    cout<<(int)ch;
    cout<<endl;

    inFile.get(ch);
    cout<<(int)ch;
    cout<<endl;

    inFile.get(ch);
    cout<<(int)ch;
    cout<<endl;

    inFile.get(ch);
    cout<<(int)ch;
    cout<<endl;

    return 0;
}


97 9
98
99
100
13
10


in the output, the second line from bottom is carriage return, the last line is new line.

Thank Duoas for your reminder.
This behavior really bugs me too... there is, I think, little excuse for such poor tracking, particularly as the newline handling and the like is performed by the C or C++ standard library.
Topic archived. No new replies allowed.