modify txt file in C

C

chaobrenda

hi everyone,
i met some problem in this.
i used fseek to locate position prepared to be written.
for example,
fseek(stream,16,seek_set)
then
fputs(time,stream)
while time will be users input.

the content of the txt file to be modified

aabb, ccddeeff, 30gghhiijjkk

obvious the string 30(at byte position 16)
is going to be changed.
but if users input were 3000
then gg(position 18) will be overwrote.
i will get the result
aabb, ccddeeff, 3000hhiijjkk

but all i need is

aabb, ccddeeff, 3000gghhiijjkk

or EVEN users input were 300000
i still need the result to be

aabb, ccddeeff, 300000gghhiijjkk

how to do it?
thanks
 
L

Lew Pitcher

hi everyone,
i met some problem in this.
i used fseek to locate position prepared to be written.
for example,
fseek(stream,16,seek_set)

You do realize that for a text stream, the offset must either be zero,
or be the value returned by a previous successful call to ftell() on
the same stream, don't you?
then
fputs(time,stream)
while time will be users input. [snip]
how to do it?
thanks

In a nutshell, you cannot do it with one file. While you can replace
in place data, the replacement data must be the same size as the data
to be replaced. You cannot "insert" data without overwriting other
data elements.

You will have to write your program to create a second file into which
you will copy data from the first file. You will first copy the data
prior to the data to be changed. You will then write the new data, and
finally, you will copy the data following the data to be changed. The
first file will not change; the unaffected data from the first file
will be copied to the second file, in sequence with the inserted data
 
M

Mark McIntyre

hi everyone,
i met some problem in this.
i used fseek to locate position prepared to be written.

This is a FAQ - 12.30, and also read 19.14

And in fact, please read the FAQ entirely...
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top