Removing characters from a file

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
 
T

Tom

Forgot to mention what some of the other functions I was using do.

long lLen = filelength(iofile) // self explanatory. Debugger shows this is
working as expected so I didn't write what that does.

savetostr(sTemp,iofile); // this function just copies the characters from
the current position in the string onwards into the temporary string. It
returns the stream pointer back to where it originally was

Thanks,
Tom
 
J

Jack Klein

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:-

[snip]

There is literally no guaranteed way to do this. It is not a
requirement of the language that it be able to produce this result,
and there are operating systems that do not permit it.

There are some particular system calls on some particular compiler/OS
combinations that might allow this to work, but it is non-standard and
non-portable.

Just rewrite the file and you code will be portable everywhere.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
T

Tom

There is literally no guaranteed way to do this. It is not a
requirement of the language that it be able to produce this result,
and there are operating systems that do not permit it.

Seems like a crazy thing to omit from an OS to me... ah well guess I'll have
to make do.
There are some particular system calls on some particular compiler/OS
combinations that might allow this to work, but it is non-standard and
non-portable.

Just rewrite the file and you code will be portable everywhere.


Thanks for the response,
Tom
 
M

Mike Wahler

Tom said:
Seems like a crazy thing to omit from an OS to me...

All operating systems do not serve the same purpose.
Making such generalizations about all of them doesn't
make sense. E.g. some computer systems don't have
'files' at all. I've worked with a few.
ah well guess I'll have
to make do.

If your operating system has a file system, it will have
an interface (API) for your program to use to do whatever
it allows. But this is outside the domain of the C++
language. Check your OS documentation.

Also, in your original post, you talked about "e.g. ini files".
I'm guessing these are 'configuration information' as in Windows
..ini files. Generally these files are relatively small, and
rewriting them completely will not take noticably longer than
trying to edit them 'in place'. I suspect you're anticipating
a performance problem that doesn't exist.

Only optimize after you've *proven*, via profiling, or customer
feedback, that performance really is a problem.

-Mike
 
T

Tom

There are some particular system calls on some particular compiler/OS
Also, in your original post, you talked about "e.g. ini files".
I'm guessing these are 'configuration information' as in Windows
.ini files. Generally these files are relatively small, and
rewriting them completely will not take noticably longer than
trying to edit them 'in place'. I suspect you're anticipating
a performance problem that doesn't exist.

Yes I just have a preconceived idea that it was an issue and I guess it just
seemed more logical to me that I'd be able to edit files. I don't want to do
too much OS specific programming, particularly for something not interface
related, so I think I'll stick to the 'create new file' idea rather than
moving into system calls.

Thanks again,
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top