Weird behaviour by get() and seekg()

Normally I find everything I need to know about C++ on the internet , but now I'm having a hard time.

Description: After setting the get pointer by seekg(int) , I use get() to read next character in the file. This operation causes the pointer to move not by 1 position but by much more (the more text in the .txt file the further it moves).
By manipulating text file size I was able to make it move anywhere from 3 to 15 additional positions. I'd like to eliminate this "feature" because the program I'm writing is suposed to read text files of different size.

The code:
http://pastebin.com/bzSAPJcv

The output:
http://pastebin.com/51eEnLtj

As you can see the pointer moves from 0 to 6 position during get() and then from 4 to 10 position.

Thanks for help in advance.
Last edited on
As you can see the pointer moves from 0 to 6 position during get() and then from 4 to 10 position.


Nope. That's not what I see. According to the code/input file supplied, the output corresponds to 6 being extracted from position 0, then the pointer advances to position t.

Might help to get the actual output, the actual input, or the actual code, because as they are now they just don't sync up.

My output from your code and the input file you gave:

0z1t2

6


Which is exactly what I would expect. On my system a line break in a file is signified by a newline and carriage return character so I would expect the pointer to go from 4 to 6 when reading one.
Last edited on
txt file: http://www.sendspace.com/file/ggztha


the code is exactly as above.
my output is following:
0r7n8
r11

Last edited on
I'm not sure why the return value from tellg concerns you so much. Seeking to arbitrary locations in file opened in text mode as you do in your code isn't likely to work correctly.

You may seek to the beginning of a text file. Seek to the end of a text file, or seek to a location earlier returned by a tellg on that file. Does it not work in those cases?

What exactly are you trying to accomplish?

Also, quit using shitty file hosting sites with fake download links. Just post the text of the files here.
Last edited on
Well the link works if you click the correct one.
not shitty link : http://pastebin.com/Lahb9H8d

I'm trying to accomplish following:
write pointer position to the variable pos then move 'forward' using get() and after that come back to the original position pos by seekg. It does work as intended except when I use get() upon returning to the original position the pointer 'jumps' by a couple of spaces.

for example: pointer is at 175 , moving to 190 by get() loop and then seekg(175). I use get() and I'm at 184 and not 176. This is troublesome for me cause more often than not it 'jumps' over newlines , which I want to count incrementally.

It's visible in my output:
seekg to 0 , get() to 7
seekg to 4 , get() to 11
Last edited on
What's visible in your test code/output is that you seek to a position in the file that wasn't returned by tellg() and it doesn't do what you expect, which isn't surprising. If you're doing the same thing in the code you're using elsewhere, I would also not be surprised (although I would be surprised if that was actually what you were doing.)

Because you're having trouble with newlines, I suspect you may be operating on files generated with a different line break indicator than is the norm on your system. Might that be the case?
I'm getting position of pointer by tellg and instantly moving the cursor there. So relatively it's not (shouldn't be) moving at all.
pos=file.tellg(). position:x
file.seekg(pos) position:x
file.get() position: x+random number greater than 1

Well ,that might be a case ,however , I have no idea how to check those line break indicators.

My program is really about parsing infromation from .txt file and sending it to an .xls file.
I've never done anything like that and maybe commong practices are different.

Maybe I should read whole .txt fail to the buffer beforehand? What do you think?
Last edited on
If you are looking to seek/read a file byte-by-byte then perhaps open it in binary mode?
No , I read it char by char. And possibly edit obtained strings.

I sent file to stringstream object and compared effects of seekg and get in those two streams.

stringstream filestr;
ifstream file;
// 10 is arbitrary number , it happens for 33 , 50 , 100 etc.
filestr.seekg(10);
filestr.get();
cout<<filestr.tellg(); //o: 11
file.seekg(10);
file.get();
cout<<file.tellg(); //o: 25

Now , nobody can tell me this behaviour of get() is normal. I think I'm going to write program using stringstream and avoid this nuisance , but if anybody can help with sorting this problem out - feel free to do so.
Last edited on
Opening and reading a file in text mode is different than in binary mode. If you really want a one-at-a-time read you need to open the file in binary mode. This also means you can capture line feeds, tabs, etc.
No , I read it char by char. And possibly edit obtained strings.


The only difference between binary mode and text mode is the translation of line break terminators. In text mode the translation happens. In binary mode it doesn't.

It does appear there is a bug (or was) at some point with gcc 4.7.1 windows ports that caused the seekx/tellx functions to be inconsistent in text modes.

From: ftp://ftp.fu-berlin.de/pc/languages/djgpp/current/v2gnu/gcc471.README
Note for users of C++ IO classes fstream, ifstream, ofstream
============================================================

There is a regression against earlier versions of GCC (gcc-2.95.3 and
earlier): Member functions tellp(), tellg(), seekp() and seekg()
are broken when stream is opened not in binary mode. If You are going
to use any similar functions You should open stream in binary mode.


Related:
http://stackoverflow.com/questions/12252075/ifstream-tellg-not-returning-the-correct-position

Topic archived. No new replies allowed.