Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   modify txt file in C (http://www.velocityreviews.com/forums/t559749-modify-txt-file-in-c.html)

chaobrenda@gmail.com 12-11-2007 06:10 PM

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

Lew Pitcher 12-11-2007 06:23 PM

Re: modify txt file in C
 
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



Mark McIntyre 12-11-2007 10:58 PM

Re: modify txt file in C
 
chaobrenda@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>


All times are GMT. The time now is 01:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.