954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Weird, weird problems with fstream

Hi,

I have a somewhat odd and complicated question. Some background: I am trying to open up a file which contains formatted numerical data, find certain elements, and then overwrite these elements with corrected data.

So, my plan was to just iterate until I found the data, use .tellg() to get my the pointer, use .seekp() to find where I need to overwrite the existing data and then just simply .put() whatever I wanted. Simple enough right?

Then, I ran into some problems.

#1)Seekp() wasn't affecting where my .put() was going. Not at all. After a few hours, I fiddled around and found that seekg() was actually dictating where my .put() was going to be written. Here's where we descent into the twilight zone.

#2) Alright, I can deal with the strange reversal of seekg and seekp. Now, I find that wherever I .put() is something like 90 characters PREVIOUS to where my pointer should be. I tested this by setting my seekp and seekg to the same spot, reading whatever was there, setting the pointers back, then trying to put() a character. It invaribly showed up 98 characters before wherever I expected it to be.

#3) OK, I can deal with this too. I think. I'll just compensate for this weird offset right? Everything's going fine UNTIL.....

I realize that the space between where I want my pointer to be and where it actually is (The weird negative offset) changes. After five or six cycles of beautiful correct search+replacement, it fizzes out by one character and starts overwriting totally wrong stuff. This makes me sad.

One of my ideas of how to fix it requires the deletion of characters in a file. Actual, deletion... not overwriting with null or space. If I can actually delete a character, I think I can compensate for this weird offset.

So my questions are threefold:
1) Is there any way to actually DELETE a character in a file with C++?
2) What's with this weird offset and reversal of seekg/seekp?
3) Could all my problems arise from using fstreams as global variables?

Some of my mindless conjecture:
I declate my fstreams globally as such:

fstream statsFS;

and then open them in a function:

void reclassOne()
{
        int status;
        
        statsFS.open("3_statistics.dat", ios::out|ios::in);
        minmaxFS.open("3_minmax.dat", ios::out|ios::in);
}
Analogsleeper
Newbie Poster
13 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

The usual way is to read and rewrite.
http://www.eskimo.com/~scs/C-faq/q19.14.html

Seeking on files opened in text mode may be precarious.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

I was hoping it wouldn't come to that... some of my files are 400+ MB...

What about other programming languages? Any of them offer simple file manipulation?

Analogsleeper
Newbie Poster
13 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Any language will have much the same problem, I believe. If you try to replace "123" with "4567" or "89", how do you handle it?

[edit] http://www.parashift.com/c++-faq-lite/serialization.html

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You