File input, get stream pointer's position

Started by
0 comments, last by Bregma 11 years, 6 months ago
Hi, I'm writing a program where I have to manipulate the stream pointer in an input file. I wrote a testing program, I wanted to test if I get the right pointer position in a file where I have written few strings in a single line.

The contents of the file are "hello world foo(int) a=b".
The code is

#include <iostream>
using namespace std;
#include <fstream>
int main()
{
ifstream Infile;
char c;
Infile.open("Input.txt");
if (Infile.is_open())
{
Infile.seekg(0, ios::beg);
while (!Infile.eof())
{
cout << Infile.tellg() << ' ';
Infile.get(c);
}
}
return 0;
}

The output of the program is
0 2 4 6 8 10 12 14 16 18 20 22 24 26

However, should'nt it be
0 1 2 3 4 5 6 7 8 9 10 11 12 13 and so on....

Because I am reading one character at a time and from what I know, the get(char) function also reads whitespaces and endlines...

I don't understand why the position pointer is being incremented by two and not one!!? However if manipulate the pointer using seekg() func, it returns the character at that position but If i use tellg() and just read input, then it increments the pointer by two...
Any clues?
Advertisement

I don't understand why the position pointer is being incremented by two and not one!!? However if manipulate the pointer using seekg() func, it returns the character at that position but If i use tellg() and just read input, then it increments the pointer by two...
Any clues?

Perhaps the input file is using UTF-16 or UCS-2 encoding?

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement