Here's the code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string infile(argv[1]);
ifstream fin(infile.data());
string var_name;
char ch = fin.get();
cout << ch << endl;
ch = fin.get();
cout << ch << endl;
ch = fin.get();
cout << ch << endl;
cout << "pos: " << fin.tellg() << endl;
fin.seekg(-sizeof(char),ios::cur);
cout << "pos: " << fin.tellg() << endl;
ch = fin.get();
cout << ch << endl;
return 0;
}
the file content is just a string:
<
?
x
m
and the output is:
<\n
?\n
x\n
pos: 3\n
pos: 2
x
Why the last character printed is still 'x' ?? Why doesn't seekg function move the file pointer back for one byte?