Jump to content
  • Advertisement
Sign in to follow this  
uzipaz

File input, get stream pointer's position

This topic is 2122 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!