468,513 Members | 1,814 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

modify txt file in C

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
Dec 11 '07 #1
2 6712
On Dec 11, 1:10 pm, chaobre...@gmail.com wrote:
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
Dec 11 '07 #2
ch********@gmail.com wrote:
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...

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Dec 11 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Chris McKeever | last post: by
12 posts views Thread by Venkat | last post: by
3 posts views Thread by Maileen | last post: by
3 posts views Thread by muttu2244 | last post: by
4 posts views Thread by muttu2244 | last post: by
1 post views Thread by LB | last post: by
1 post views Thread by TimEl | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.