// get pointer positions with for
#include <iostream>
#include <fstream>
usingnamespace 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 ?
// get pointer positions with while
#include <iostream>
#include <fstream>
usingnamespace 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,... ?
"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
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.
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."
// get pointer positions with while
#include <iostream>
#include <fstream>
usingnamespace 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