Get Pointer - where and why?

I have read and practiced through C++ Language Tutorial of this site.
http://www.cplusplus.com/doc/tutorial/

I have just been studying the last chapter: Input/Output with files
http://www.cplusplus.com/doc/tutorial/files/


I have tried to play with the getpointer, but I can't understand how it exactly works.


What is happening here at the for loop? (using Code::Blocks)

Step1: I move the getpointer to position zero. - myfile.seekg(0)

Step2: I ask where the getpointer is - myfile.tellg()
And it prints that its position is 1.

Step3: But when I use (char)myfile.get() function
it reads the character at position zero.


rexample.txt - one line only
 
0123rex789


My Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  long begin,end;
  ifstream myfile ("rexample.txt");

  begin = myfile.tellg();   cout<<"begin: "<<begin<<endl;

  myfile.seekg (0, ios::end);
  end = myfile.tellg();     cout<<"end:   "<<end  <<endl;

  cout << "size is: " << (end-begin) << " bytes.\n"<<endl;

  for(int i=0;i<=end;i++) {
    myfile.seekg(i);
    cout<<"myfile.seekg ("<<i<<") => "<<myfile.tellg()
        <<" - myfile.get() = "<<(char)myfile.get()<<endl;
  }

  myfile.close();
  return 0;
}
begin: 0
end:   10
size is: 10 bytes.

myfile.seekg (0) => 1 - myfile.get() = 0
myfile.seekg (1) => 2 - myfile.get() = 1
myfile.seekg (2) => 3 - myfile.get() = 2
myfile.seekg (3) => 4 - myfile.get() = 3
myfile.seekg (4) => 5 - myfile.get() = r
myfile.seekg (5) => 6 - myfile.get() = e
myfile.seekg (6) => 7 - myfile.get() = x
myfile.seekg (7) => 8 - myfile.get() = 7
myfile.seekg (8) => 9 - myfile.get() = 8
myfile.seekg (9) => 10 - myfile.get() = 9
myfile.seekg (10) => -1 - myfile.get() = ˙
tellg() and get() is called in the same expression so you can't be sure which one will be called first.
Agreed.

Try this:
1
2
3
4
5
6
7
8
9
  for (int i=0; i<end; i++)
  {
        myfile.seekg(i, ios::beg);
        long where = myfile.tellg();
        char ch = myfile.get();

        cout << "myfile.seekg (" << i << ") => " << where
             << " - myfile.get() = " << ch << endl;
  }
I always learn new things on this forum. The best.

I've never thought of that functions also have precedence like the operators.

You are right. It is working now. I'm very happy.

Chervil && Peter87,
thank you very much for your suggestions.

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
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  long begin,end;
  ifstream myfile ("rexample.txt");

  begin = myfile.tellg();   cout<<"begin: "<<begin<<endl;

  myfile.seekg (0, ios::end);
  end = myfile.tellg();     cout<<"end:   "<<end  <<endl;

  cout << "size is: " << (end-begin) << " bytes.\n"<<endl;

/*  for(int i=0;i<=end;i++) {
    myfile.seekg(i);
    cout<<"myfile.seekg ("<<i<<") => "<<myfile.tellg()
        <<" - myfile.get() = "<<(char)myfile.get()<<endl;
  }
*/

// tellg() and get() is called in the same expression
// so you can't be sure which one will be called first.

  for (int i=0; i<end; i++)
  {
        myfile.seekg(i, ios::beg);
        long where = myfile.tellg();
        char ch = myfile.get();

        cout << "myfile.seekg (" << i << ") => " << where
             << " - myfile.get() = " << ch << endl;
  }

  myfile.close();
  return 0;
}
begin: 0
end:   10
size is: 10 bytes.

myfile.seekg (0) => 0 - myfile.get() = 0
myfile.seekg (1) => 1 - myfile.get() = 1
myfile.seekg (2) => 2 - myfile.get() = 2
myfile.seekg (3) => 3 - myfile.get() = 3
myfile.seekg (4) => 4 - myfile.get() = r
myfile.seekg (5) => 5 - myfile.get() = e
myfile.seekg (6) => 6 - myfile.get() = x
myfile.seekg (7) => 7 - myfile.get() = 7
myfile.seekg (8) => 8 - myfile.get() = 8
myfile.seekg (9) => 9 - myfile.get() = 9
closed account (zb0S216C)
"std::ifstream::tellg( )" and "std::ifstream::seekg( )" only work in "std::ios::binary" mode.

Wazzak
Last edited on
@Framework

In this tutorial http://www.cplusplus.com/doc/tutorial/files/
they are explained before binary files.

BTW in case of the above example it looks like that it works with simple text files too.


BUT here is a problem that I can't understand:

http://www.cplusplus.com/forum/beginner/95131/


Last edited on
rexample.txt
1
2
3
4
5
6
0123rex789
253
2546456
12321
23fddf
sdfsdf


Here for example the position 10 and 11 have a value of dec 10 (hex 0A) of Ascii code which is LF --> http://en.wikipedia.org/wiki/Newline
according to The standard ASCII table --> http://www.cplusplus.com/doc/ascii/

Ascii codes of the characters pointed by the getpointer
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  long begin,end;
  ifstream myfile ("rexample.txt");

  begin = myfile.tellg();   cout<<"begin: "<<begin<<endl;

  myfile.seekg (0, ios::end);
  end = myfile.tellg();     cout<<"end:   "<<end  <<endl;

  cout << "size is: " << (end-begin) << " bytes.\n"<<endl;

/*  for(int i=0;i<=end;i++) {
    myfile.seekg(i);
    cout<<"myfile.seekg ("<<i<<") => "<<myfile.tellg()
        <<" - myfile.get() = "<<(char)myfile.get()<<endl;
  }
*/

// tellg() and get() is called in the same expression
// so you can't be sure which one will be called first.

  for (int i=0; i<end; i++)
  {
        myfile.seekg(i, ios::beg);
        long where = myfile.tellg();
        int ch = myfile.get();

        cout << "myfile.seekg (" << i << ") => " << where
             << " - myfile.get() = " << ch << endl;
  }

  myfile.close();
  return 0;
}
begin: 0
end:   49
size is: 49 bytes.

myfile.seekg (0) => 0 - myfile.get() = 48
myfile.seekg (1) => 1 - myfile.get() = 49
myfile.seekg (2) => 2 - myfile.get() = 50
myfile.seekg (3) => 3 - myfile.get() = 51
myfile.seekg (4) => 4 - myfile.get() = 114
myfile.seekg (5) => 5 - myfile.get() = 101
myfile.seekg (6) => 6 - myfile.get() = 120
myfile.seekg (7) => 7 - myfile.get() = 55
myfile.seekg (8) => 8 - myfile.get() = 56
myfile.seekg (9) => 9 - myfile.get() = 57
myfile.seekg (10) => 10 - myfile.get() = 10
myfile.seekg (11) => 11 - myfile.get() = 10
myfile.seekg (12) => 12 - myfile.get() = 50
myfile.seekg (13) => 13 - myfile.get() = 53
myfile.seekg (14) => 14 - myfile.get() = 51
myfile.seekg (15) => 15 - myfile.get() = 10
myfile.seekg (16) => 16 - myfile.get() = 10
myfile.seekg (17) => 17 - myfile.get() = 50
myfile.seekg (18) => 18 - myfile.get() = 53
myfile.seekg (19) => 19 - myfile.get() = 52
myfile.seekg (20) => 20 - myfile.get() = 54
myfile.seekg (21) => 21 - myfile.get() = 52
myfile.seekg (22) => 22 - myfile.get() = 53
myfile.seekg (23) => 23 - myfile.get() = 54
myfile.seekg (24) => 24 - myfile.get() = 10
myfile.seekg (25) => 25 - myfile.get() = 10
myfile.seekg (26) => 26 - myfile.get() = 49
myfile.seekg (27) => 27 - myfile.get() = 50
myfile.seekg (28) => 28 - myfile.get() = 51
myfile.seekg (29) => 29 - myfile.get() = 50
myfile.seekg (30) => 30 - myfile.get() = 49
myfile.seekg (31) => 31 - myfile.get() = 10
myfile.seekg (32) => 32 - myfile.get() = 10
myfile.seekg (33) => 33 - myfile.get() = 50
myfile.seekg (34) => 34 - myfile.get() = 51
myfile.seekg (35) => 35 - myfile.get() = 102
myfile.seekg (36) => 36 - myfile.get() = 100
myfile.seekg (37) => 37 - myfile.get() = 100
myfile.seekg (38) => 38 - myfile.get() = 102
myfile.seekg (39) => 39 - myfile.get() = 10
myfile.seekg (40) => 40 - myfile.get() = 10
myfile.seekg (41) => 41 - myfile.get() = 115
myfile.seekg (42) => 42 - myfile.get() = 100
myfile.seekg (43) => 43 - myfile.get() = 102
myfile.seekg (44) => 44 - myfile.get() = 115
myfile.seekg (45) => 45 - myfile.get() = 100
myfile.seekg (46) => 46 - myfile.get() = 102
myfile.seekg (47) => 47 - myfile.get() = 10
myfile.seekg (48) => 48 - myfile.get() = 10



The characters pointed by the getpointer:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  long begin,end;
  ifstream myfile ("rexample.txt");

  begin = myfile.tellg();   cout<<"begin: "<<begin<<endl;

  myfile.seekg (0, ios::end);
  end = myfile.tellg();     cout<<"end:   "<<end  <<endl;

  cout << "size is: " << (end-begin) << " bytes.\n"<<endl;

/*  for(int i=0;i<=end;i++) {
    myfile.seekg(i);
    cout<<"myfile.seekg ("<<i<<") => "<<myfile.tellg()
        <<" - myfile.get() = "<<(char)myfile.get()<<endl;
  }
*/

// tellg() and get() is called in the same expression
// so you can't be sure which one will be called first.

  for (int i=0; i<end; i++)
  {
        myfile.seekg(i, ios::beg);
        long where = myfile.tellg();
        char ch = myfile.get();

        cout << "myfile.seekg (" << i << ") => " << where
             << " - myfile.get() = " << ch << endl;
  }

  myfile.close();
  return 0;
}
begin: 0
end:   49
size is: 49 bytes.

myfile.seekg (0) => 0 - myfile.get() = 0
myfile.seekg (1) => 1 - myfile.get() = 1
myfile.seekg (2) => 2 - myfile.get() = 2
myfile.seekg (3) => 3 - myfile.get() = 3
myfile.seekg (4) => 4 - myfile.get() = r
myfile.seekg (5) => 5 - myfile.get() = e
myfile.seekg (6) => 6 - myfile.get() = x
myfile.seekg (7) => 7 - myfile.get() = 7
myfile.seekg (8) => 8 - myfile.get() = 8
myfile.seekg (9) => 9 - myfile.get() = 9
myfile.seekg (10) => 10 - myfile.get() = 

myfile.seekg (11) => 11 - myfile.get() = 

myfile.seekg (12) => 12 - myfile.get() = 2
myfile.seekg (13) => 13 - myfile.get() = 5
myfile.seekg (14) => 14 - myfile.get() = 3
myfile.seekg (15) => 15 - myfile.get() = 

myfile.seekg (16) => 16 - myfile.get() = 

myfile.seekg (17) => 17 - myfile.get() = 2
myfile.seekg (18) => 18 - myfile.get() = 5
myfile.seekg (19) => 19 - myfile.get() = 4
myfile.seekg (20) => 20 - myfile.get() = 6
myfile.seekg (21) => 21 - myfile.get() = 4
myfile.seekg (22) => 22 - myfile.get() = 5
myfile.seekg (23) => 23 - myfile.get() = 6
myfile.seekg (24) => 24 - myfile.get() = 

myfile.seekg (25) => 25 - myfile.get() = 

myfile.seekg (26) => 26 - myfile.get() = 1
myfile.seekg (27) => 27 - myfile.get() = 2
myfile.seekg (28) => 28 - myfile.get() = 3
myfile.seekg (29) => 29 - myfile.get() = 2
myfile.seekg (30) => 30 - myfile.get() = 1
myfile.seekg (31) => 31 - myfile.get() = 

myfile.seekg (32) => 32 - myfile.get() = 

myfile.seekg (33) => 33 - myfile.get() = 2
myfile.seekg (34) => 34 - myfile.get() = 3
myfile.seekg (35) => 35 - myfile.get() = f
myfile.seekg (36) => 36 - myfile.get() = d
myfile.seekg (37) => 37 - myfile.get() = d
myfile.seekg (38) => 38 - myfile.get() = f
myfile.seekg (39) => 39 - myfile.get() = 

myfile.seekg (40) => 40 - myfile.get() = 

myfile.seekg (41) => 41 - myfile.get() = s
myfile.seekg (42) => 42 - myfile.get() = d
myfile.seekg (43) => 43 - myfile.get() = f
myfile.seekg (44) => 44 - myfile.get() = s
myfile.seekg (45) => 45 - myfile.get() = d
myfile.seekg (46) => 46 - myfile.get() = f
myfile.seekg (47) => 47 - myfile.get() = 

myfile.seekg (48) => 48 - myfile.get() = 

Topic archived. No new replies allowed.