Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > modify txt file in C

Reply
Thread Tools

modify txt file in C

 
 
chaobrenda@gmail.com
Guest
Posts: n/a
 
      12-11-2007
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 1 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
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      12-11-2007
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


 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      12-11-2007
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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file dgcosgrave@gmail.com Python 8 12-19-2012 06:29 PM
Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Jochen Brenzlinger Java 7 09-15-2011 01:23 AM
Opening a txt file to view ( i.e. readme.txt) Sameen C++ 2 08-29-2005 03:14 PM
how to modify a txt file Jerry C Programming 4 01-31-2005 06:20 AM
File Access error - writing to .txt file John Carnahan ASP .Net 2 07-18-2003 10:35 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57