Modifying files using fopen 
Author Message
 Modifying files using fopen

Hi all!

I have a file which I need to add strings to in different places. Can
anyone assist me with how to open a file using fopen and then
modifying it? I tried opening with "awt" and 'fseek'ing to the
beginning etc' but it doesnt seem
to work.
Is the only way reading the whole file into a new one and changing it
as I go along??? I hope not, could be troublesome when file gets big.

10x for any help!

Quorthon



Sun, 18 Apr 2004 15:58:51 GMT  
 Modifying files using fopen

Quote:
> Hi all!

> I have a file which I need to add strings to in different places. Can
> anyone assist me with how to open a file using fopen and then
> modifying it? I tried opening with "awt" and 'fseek'ing to the
> beginning etc' but it doesnt seem
> to work.
> Is the only way reading the whole file into a new one and changing it
> as I go along??? I hope not, could be troublesome when file gets big.

You cannot change the contents of a file except when the size
of the file doesn't change, which is something like overwrite.
(this can be done in "r+"-mode)

You really need to apply your changes as you go thru the file
and than create a new file, probably unlink the old file and
rename the the newly created file, so it looks like the old file
has been overwritten. In many programs the contents of a file is
read into memory where the changes are applied to then and are
written into the old file after truncating it (by simply opening
with "w"-mode).

Again Chris Toreks post from a few days ago may be helpful here.

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Sun, 18 Apr 2004 17:41:07 GMT  
 Modifying files using fopen

Quote:
>I have a file which I need to add strings to in different places. Can
>anyone assist me with how to open a file using fopen and then
>modifying it?

Nope.  All you can do to an existing file in C is to overwrite parts of
it or to append to it.  No arbitrary insertions or deletions are easily
possible.

Quote:
>I tried opening with "awt" and 'fseek'ing to the
>beginning etc' but it doesnt seem to work.

'a' and 'w' are mutually exclusively mode specifiers, i.e. you can use
only one of them.  Also note that text files cannot even be partially
overwritten, only appended to or completely overwritten.

Quote:
>Is the only way reading the whole file into a new one and changing it
>as I go along??? I hope not, could be troublesome when file gets big.

If this approach is not possible, because of lack of memory and disk
space, it is possible to modify the file in place, but it is both
clumsy and inefficient.  Let's say you want to insert 3 characters.
You append three characters at the end of the file, to make place for
the new characters.  You move the contents of the file 3 characters
back, from the point of insertion to the (previous) end of the file
(this is the clumsy and inefficient part, because there is no easy way
of doing it) and then write the 3 new characters in the right place.

Again, the file must be opened in binary mode.

Dan
--
Dan Pop
CERN, IT Division

Mail:  CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland



Mon, 19 Apr 2004 00:52:12 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Using fopen when you don't know the name of the file

2. Help - code using fopen() failing on production system - file may already be open

3. Opening a file using fopen

4. convert memory buffer directly to FILE * without using fopen()

5. HELP!! Need to Modify ini file using C

6. The make file has been modified and cannot be used as an internal workspace

7. Saving files in C (FILE, fopen, fclose)

8. using fopen to talk to a UNIX device.

9. using fopen

10. Problems using fopen from a NT service instead of CreateFile

11. using fopen, fread, getc, putc, etc in Windows 95

12. fopen - Can a wild card be used?

 

 
Powered by phpBB® Forum Software