File Content Modification

T

Tinku

Dear C++'ers

I like to know how we can modify the contents in a file

ex: There is a file.txt

with the following data

Weather=Cold
WhatIDo=SleepOntheBed

to be replaced with

Weather=Sunny
WhatIDo=GoToBeach

The seekp function able to replace the contents but it also
eliminating the further contents in the buffer. Is there any way we
read only one line of the file ata time, adjust the size of this line
to fit the new contents.
For my application putting all the file contents in an array is not
feasible.

Hope I am clear
Waiting for ur replies
Cheers
P
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Dear C++'ers

I like to know how we can modify the contents in a file

ex: There is a file.txt

with the following data

Weather=Cold
WhatIDo=SleepOntheBed

to be replaced with

Weather=Sunny
WhatIDo=GoToBeach

The seekp function able to replace the contents but it also
eliminating the further contents in the buffer. Is there any way we
read only one line of the file ata time, adjust the size of this line
to fit the new contents.
For my application putting all the file contents in an array is not
feasible.

An alternative is to open a second, temporary file, to which you write
the changes and when done replace the original file with the temporary
one.
 
G

Gianni Mariani

Tinku said:
Dear C++'ers

I like to know how we can modify the contents in a file

ex: There is a file.txt

with the following data

Weather=Cold
WhatIDo=SleepOntheBed

to be replaced with

Weather=Sunny
WhatIDo=GoToBeach

The seekp function able to replace the contents but it also
eliminating the further contents in the buffer. Is there any way we
read only one line of the file ata time, adjust the size of this line
to fit the new contents.
For my application putting all the file contents in an array is not
feasible.

It would be easy if all the lines were the same length ! Remember the
80 column text files from IBM mainframe days ?

OK, so maybe not.

An alternative is to rewrite the entire file.
 
T

Tinku

Tinkuwrote:




It would be easy if all the lines were the same length ! Remember the
80 column text files from IBM mainframe days ?

OK, so maybe not.

An alternative is to rewrite the entire file.

Thanks all for replying me...

I missed one important requirement.
I have to modify lines randomly in the file by taking those particular
lines as parameters. There is one nasty function doing this stuff. It
scans the file until that line has reached and try to adjust the file
pointer to the appropriate position and modify the content there. So
using a temp file could not be a feasible solution to my problem as
only one global file has to be maintained throughout the software
run.

I like to know if there is a way to read one line at a time (ex:
getline()) modify it and put the new content into this line without
disturbing the content in the next line. Any suggestions/ ideas in
this direction will be very much appreciated.

Thanking you

Cheers
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top