473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Removing characters from a file

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
Jul 19 '05 #1
5 8009
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
Jul 19 '05 #2
On Tue, 30 Sep 2003 13:27:14 +1000, "Tom" <a@a.com> wrote in
comp.lang.c++:
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
Jul 19 '05 #3
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
Jul 19 '05 #4

"Tom" <a@a.com> wrote in message news:3f**********@news.iprimus.com.au...
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.
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.
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
Jul 19 '05 #5
Tom
> > > 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
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: hokiegal99 | last post by:
This script works as I expect, except for the last section. I want the last section to actually remove all spaces from the front and/or end of filenames. For example, a file that was named " test ...
1
by: Ralph Noble | last post by:
Folks ... I have a pipe-delimited ASCII text file with a lot of different non-printing characters. Rather than try and figure out all the non-printing characters that exist in this 17+ million...
8
by: Peter O'Reilly | last post by:
I have an HTML form with a textarea input box. When the user conducts a post request (e.g. clicks the submit button), an HTML preview page is presented to them with the information they have...
11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
2
by: caspardh | last post by:
i was trying to code a morse code decipherer which workd fine except that the .txt file that i was converting had linebreaks built in, i cant get python to ignore the line breaks and i cant find any...
3
by: Yobbo | last post by:
Hi All I have an ASP function in place to strip invalid chars out of a data store before I create an XML file of this data, but my function doesn't work on a certain set of chars. As far as I...
0
by: Benny the Guard | last post by:
I have a string value that may contain some unprintable characters. I am using the xml.sax.escape to remove the the <, > and & characters fine but it seems to leave in the \n characters. Later this...
13
by: Eps | last post by:
Hi there, I believe all strings in .net are unicode by default, I am looking for a way to remove all non ascii characters from a string (or optionally replace them). There is an article on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.