Jump to content

  • Log In with Google      Sign In   
  • Create Account


File input, get stream pointer's position


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 uzipaz   Members   -  Reputation: 198

Like
0Likes
Like

Posted 25 September 2012 - 07:57 AM

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?

Sponsor:

#2 Bregma   Crossbones+   -  Reputation: 4361

Like
0Likes
Like

Posted 25 September 2012 - 10:12 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS