Getpointer positions and characters it points to

Input/Output with files
http://www.cplusplus.com/doc/tutorial/files/


I try to get the getpointer position and the character it points to.

I try for loop and while loop (using Code::Blocks).



example_seek.txt contains:
This is LINE 01.
This is LINE 02.



For loop:
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
// get pointer positions with for

#include <iostream>
#include <fstream>
using namespace std;

int main () {

    long begin,end;
    ifstream myfile ("example_seek.txt");

    myfile.seekg (0, ios::end);
    end = myfile.tellg();
    myfile.seekg (0, ios::beg);

    for (long i=0; i<=end; i++) {
        myfile.seekg (i, ios::beg);
        cout<<"myfile.tellg() "<<myfile.tellg()<<' ';
        cout<<(char)myfile.get()<<endl;
    }

    myfile.close();

    return 0;
}
myfile.tellg() 0 T
myfile.tellg() 1 h
myfile.tellg() 2 i
myfile.tellg() 3 s
myfile.tellg() 4  
myfile.tellg() 5 i
myfile.tellg() 6 s
myfile.tellg() 7  
myfile.tellg() 8 L
myfile.tellg() 9 I
myfile.tellg() 10 N
myfile.tellg() 11 E
myfile.tellg() 12  
myfile.tellg() 13 0
myfile.tellg() 14 1
myfile.tellg() 15 .
myfile.tellg() 16 

myfile.tellg() 17 

myfile.tellg() 18 T
myfile.tellg() 19 h
myfile.tellg() 20 i
myfile.tellg() 21 s
myfile.tellg() 22  
myfile.tellg() 23 i
myfile.tellg() 24 s
myfile.tellg() 25  
myfile.tellg() 26 L
myfile.tellg() 27 I
myfile.tellg() 28 N
myfile.tellg() 29 E
myfile.tellg() 30  
myfile.tellg() 31 0
myfile.tellg() 32 2
myfile.tellg() 33 .
myfile.tellg() 34 


What represents the chars at the end of these lines?

myfile.tellg() 16

myfile.tellg() 17

myfile.tellg() 34

After 16 and 34 maybe \n ?
But I pressed the Enter in the file txt after the first line only.

And what stands after 17?

Why are spaces (new lines) before and after the line myfile.tellg() 17 ?





While loop:
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
// get pointer positions with while

#include <iostream>
#include <fstream>
using namespace std;

int main () {

    long begin,end;
    ifstream myfile ("example_seek.txt");

    myfile.seekg (0, ios::end);
    end = myfile.tellg();
    myfile.seekg (0, ios::beg);

    while (myfile.good()) {
        cout<<"myfile.tellg() "<<myfile.tellg()<<' ';
        cout<<(char)myfile.get()<<endl;
    }

    myfile.close();

    return 0;
}
myfile.tellg() 0 T
myfile.tellg() 2 h
myfile.tellg() 3 i
myfile.tellg() 4 s
myfile.tellg() 5  
myfile.tellg() 6 i
myfile.tellg() 7 s
myfile.tellg() 8  
myfile.tellg() 9 L
myfile.tellg() 10 I
myfile.tellg() 11 N
myfile.tellg() 12 E
myfile.tellg() 13  
myfile.tellg() 14 0
myfile.tellg() 15 1
myfile.tellg() 16 .
myfile.tellg() 17 

myfile.tellg() 18 T
myfile.tellg() 19 h
myfile.tellg() 20 i
myfile.tellg() 21 s
myfile.tellg() 22  
myfile.tellg() 23 i
myfile.tellg() 24 s
myfile.tellg() 25  
myfile.tellg() 26 L
myfile.tellg() 27 I
myfile.tellg() 28 N
myfile.tellg() 29 E
myfile.tellg() 30  
myfile.tellg() 31 0
myfile.tellg() 32 2
myfile.tellg() 33 .
myfile.tellg() 34 


Why does not start this list with getpointer positions 0,1,2,3,... ?


std::istream::get
see code: Example
http://www.cplusplus.com/reference/istream/istream/get/

Output your characters as integers instead of characters. Then consult athe ASCII table. F.e.
man ascii
Thanks for the tip.

The standard ASCII table defines 128 character codes:
http://www.cplusplus.com/doc/ascii/

Dec: 10 --> Hex: 0A --> LF

"Systems based on ASCII or a compatible character set use either
LF (Line feed, '\n', 0x0A, 10 in decimal) or
CR (Carriage return, '\r', 0x0D, 13 in decimal) individually, or
CR followed by LF (CR+LF, '\r\n', 0x0D0A).
These characters are based on printer commands:
The line feed indicated that one line of paper should
feed out of the printer thus instructed the printer
to advance the paper one line
, and a carriage return indicated
that the printer carriage should return to the beginning of the current line."
http://en.wikipedia.org/wiki/Newline

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
// get pointer positions with for

#include <iostream>
#include <fstream>
using namespace std;

int main () {

    long begin,end;
    ifstream myfile ("example_seek.txt");

    myfile.seekg (0, ios::end);
    end = myfile.tellg();
    myfile.seekg (0, ios::beg);

    for (long i=0; i<=end; i++) {
        myfile.seekg (i, ios::beg);
        cout<<"myfile.tellg() "<<myfile.tellg()<<' ';
        cout<<myfile.get()<<endl;
    }

    myfile.close();

    return 0;
}
myfile.tellg() 0 84
myfile.tellg() 1 104
myfile.tellg() 2 105
myfile.tellg() 3 115
myfile.tellg() 4 32
myfile.tellg() 5 105
myfile.tellg() 6 115
myfile.tellg() 7 32
myfile.tellg() 8 76
myfile.tellg() 9 73
myfile.tellg() 10 78
myfile.tellg() 11 69
myfile.tellg() 12 32
myfile.tellg() 13 48
myfile.tellg() 14 49
myfile.tellg() 15 46
myfile.tellg() 16 10
myfile.tellg() 17 10
myfile.tellg() 18 84
myfile.tellg() 19 104
myfile.tellg() 20 105
myfile.tellg() 21 115
myfile.tellg() 22 32
myfile.tellg() 23 105
myfile.tellg() 24 115
myfile.tellg() 25 32
myfile.tellg() 26 76
myfile.tellg() 27 73
myfile.tellg() 28 78
myfile.tellg() 29 69
myfile.tellg() 30 32
myfile.tellg() 31 48
myfile.tellg() 32 50
myfile.tellg() 33 46
myfile.tellg() 34 -1


BUT what means -1 Ascii at the last line?
I think that -1 corresponds to eof
See http://www.cplusplus.com/reference/istream/istream/get/.
You got EOF from get() indicating end of input. The reason is your loop termination condition in line 16. Use < instead of <=. EOF is not part of the file. Its generated by the iostream system to indicate some error conditions.
You are right.
http://www.cplusplus.com/reference/cstdio/EOF/

constant
EOF <cstdio>
End-of-File
"It is a macro definition of type int that expands into a negative integral constant expression (generally, -1).

It is used as the value returned by several functions in header <cstdio> to indicate that the End-of-File has been reached or to signal some other failure conditions.

It is also used as the value to represent an invalid character."

Thanks.
Last edited on
My last quetion to this is:

Why does not start this list with getpointer positions 0,1,2,3,... ?
Why only 0,2,3, ... ?

What happens to the 1?


While loop:
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
// get pointer positions with while

#include <iostream>
#include <fstream>
using namespace std;

int main () {

    long begin,end;
    ifstream myfile ("example_seek.txt");

    myfile.seekg (0, ios::end);
    end = myfile.tellg();
    myfile.seekg (0, ios::beg);

    while (myfile.good()) {
        cout<<"myfile.tellg() "<<myfile.tellg()<<' ';
        cout<<(char)myfile.get()<<endl;
    }

    myfile.close();

    return 0;
}
myfile.tellg() 0 T
myfile.tellg() 2 h
myfile.tellg() 3 i
myfile.tellg() 4 s
myfile.tellg() 5  
myfile.tellg() 6 i
myfile.tellg() 7 s
myfile.tellg() 8  
myfile.tellg() 9 L
myfile.tellg() 10 I
myfile.tellg() 11 N
myfile.tellg() 12 E
myfile.tellg() 13  
myfile.tellg() 14 0
myfile.tellg() 15 1
myfile.tellg() 16 .
myfile.tellg() 17 

myfile.tellg() 18 T
myfile.tellg() 19 h
myfile.tellg() 20 i
myfile.tellg() 21 s
myfile.tellg() 22  
myfile.tellg() 23 i
myfile.tellg() 24 s
myfile.tellg() 25  
myfile.tellg() 26 L
myfile.tellg() 27 I
myfile.tellg() 28 N
myfile.tellg() 29 E
myfile.tellg() 30  
myfile.tellg() 31 0
myfile.tellg() 32 2
myfile.tellg() 33 .
myfile.tellg() 34 



Topic archived. No new replies allowed.