C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Removing characters from a file

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Tom
Guest





PostPosted: Tue Sep 30, 2003 3:27 am    Post subject: Removing characters from a file Reply with quote



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


Back to top
Tom
Guest





PostPosted: Tue Sep 30, 2003 3:31 am    Post subject: Re: Removing characters from a file Reply with quote



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


Back to top
Jack Klein
Guest





PostPosted: Tue Sep 30, 2003 3:33 am    Post subject: Re: Removing characters from a file Reply with quote



On Tue, 30 Sep 2003 13:27:14 +1000, "Tom" <a@a.com> wrote in
comp.lang.c++:

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

Back to top
Tom
Guest





PostPosted: Tue Sep 30, 2003 8:13 am    Post subject: Re: Removing characters from a file Reply with quote

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

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



Back to top
Mike Wahler
Guest





PostPosted: Tue Sep 30, 2003 4:57 pm    Post subject: Re: Removing characters from a file Reply with quote


"Tom" <a@a.com> wrote

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

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.

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

Quote:

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.

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



Back to top
Tom
Guest





PostPosted: Wed Oct 01, 2003 6:13 am    Post subject: Re: Removing characters from a file Reply with quote

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

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



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.