T
Tom
Hi all,
I've searched high and low (google, the FAQ & so on) and I can't seem to
find any way of doing this. What I am trying to do is to edit text files
(eg. ini files) and remove/replace data in the file without having to create
new files and transferring the data and so on. So one function I was trying
to build was a "removechars" function which would simply remove x chars from
the file from the current position. The code may not be done as elegant as
possible but here is what I have so far:-
int removechars(fstream iofile,long lNum)
{
long lLen = filelength(iofile), lCurr;
char *sTemp;
lCurr = iofile.tellg();
if (lLen + 1 - lCurr - lNum > 0)
sTemp = new char[lLen + 1 - lCurr - lNum];
else
return false;
iofile.seekg(lNum,ios::cur);
savetostr(sTemp,iofile);
iofile.seekg(lCurr,ios::beg);
iofile.write(sTemp,lLen - lCurr - lNum);
// Here is the problem. I can't end the file at this current position
iofile.seekg(lCurr,ios::beg);
delete [] sTemp;
return true;
}
So as noted in the code snippet, I can't work out how to alter the
terminating position of the file. I shuffle everything forward the set
number of characters but can't seem to be able to work out the finishing
touch, and subsequently, there are trailing characters. I've tried throwing
null in at the end but in a text file that just comes up as a space in my
text editor and the rest of the characters still appear behind. I'm guessing
there is some sort of way to set the eof flag at the new position I want but
so far no luck going that way.
Any help is greatly appreciated!
Tom
I've searched high and low (google, the FAQ & so on) and I can't seem to
find any way of doing this. What I am trying to do is to edit text files
(eg. ini files) and remove/replace data in the file without having to create
new files and transferring the data and so on. So one function I was trying
to build was a "removechars" function which would simply remove x chars from
the file from the current position. The code may not be done as elegant as
possible but here is what I have so far:-
int removechars(fstream iofile,long lNum)
{
long lLen = filelength(iofile), lCurr;
char *sTemp;
lCurr = iofile.tellg();
if (lLen + 1 - lCurr - lNum > 0)
sTemp = new char[lLen + 1 - lCurr - lNum];
else
return false;
iofile.seekg(lNum,ios::cur);
savetostr(sTemp,iofile);
iofile.seekg(lCurr,ios::beg);
iofile.write(sTemp,lLen - lCurr - lNum);
// Here is the problem. I can't end the file at this current position
iofile.seekg(lCurr,ios::beg);
delete [] sTemp;
return true;
}
So as noted in the code snippet, I can't work out how to alter the
terminating position of the file. I shuffle everything forward the set
number of characters but can't seem to be able to work out the finishing
touch, and subsequently, there are trailing characters. I've tried throwing
null in at the end but in a text file that just comes up as a space in my
text editor and the rest of the characters still appear behind. I'm guessing
there is some sort of way to set the eof flag at the new position I want but
so far no luck going that way.
Any help is greatly appreciated!
Tom