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);
}