Re: Opening a file for update with creation if necessary

Ara.T.Howard_at_noaa.gov
Date: 12/14/04


Date: Mon, 13 Dec 2004 18:57:39 -0700


On Mon, 13 Dec 2004, Tom Anderson wrote:

> On Mon, 13 Dec 2004, Måns Rullgård wrote:
>
>> Tom Anderson <twic@urchin.earth.li> writes:
>>
>>> On Sun, 12 Dec 2004, Bjørn Augestad wrote:
>>>
>>>> Tom Anderson wrote:
>>>>
>>>>> On Sat, 11 Dec 2004, Bjørn Augestad wrote:
>>>>>
>>>>>> Tom Anderson wrote:
>>>>>>
>>>>>>> I want to open a file for updating - mode r+. However, i'd like it to
>>>>>>> be created if it doesn't exist, as with mode w. Can i do this with
>>>>>>> fopen, or am i going to have to use open (with flags O_RDWR |
>>>>>>> O_CREAT) and fdopen?
>>>>>>
>>>>>> Check out fopen() with mode a+, possibly with an additional call to
>>>>>> fseek/rewind/fsetpos to move to the start of the file if needed.
>>>>>
>>>>> Won't that force all writes to go to the end of the file? The standard (or
>>>>> at least a standard) seems to think so:
>>>>>
>>>>> http://www.opengroup.org/onlinepubs/007908799/xsh/fopen.html
>>>>>
>>>>> Although i'm pretty sure Linux, at least, doesn't do this.
>>>>
>>>> You're right, from the man page:
>>>>
>>>> Opening a file with append mode (a as the first character in the mode
>>>> argument) causes all subsequent writes to the file to be forced to the
>>>> then current end-of-file, regardless of intervening calls to fseek().
>>>
>>> Which OS was that on? The man page on OS X (ie FreeBSD) says something
>>> similar; the one on Linux (Debian 2.4.28) says "Open for writing. The file
>>> is created if it does not exist. The stream is positioned at the end of
>>> the file." - nothing about forcing writes to EOF. Those crazy Linux
>>> hippies!
>>
>> The man page on my Linux system has this paragraph:
>>
>> Opening a file in append mode (a as the first character of
>> mode) causes all subsequent write operations to this stream to
>> occur at end-of-file, as if preceded by an
>> fseek(stream,0,SEEK_END);
>> call.
>
> Hmm. The man page for fopen on mine has nothing at all like that; perhaps
> we have different versions of glibc or something. The man page for open
> says:
>
> The file is opened in append mode. Before each
> write, the file pointer is positioned at the end of
> the file, as if with lseek.
>
> And when i actually get of my arse and try it, i see that fopening with
> mode "a" does indeed get you the 'correct' force-to-end behaviour. ISTR
> reading something about Linux not doing this; that might be an old version
> or something. So it goes.

this seems to work

   jib:~/eg/c > cat a.c
   #include <stdlib.h>
   #include <stdio.h>
   #include <unistd.h>
   #include <sys/types.h>
   int
   main (argc, argv)
        int argc;
        char **argv;
   {
     char *path, *word;
     FILE *fs;
     fpos_t pos;

     path = argv[1] == NULL ? "foobar" : argv[1];
     word = argv[2] == NULL ? "foobar" : argv[2];
     fs = fopen (path, "a+");
     ftruncate (fileno (fs), 0);
     rewind (fs);
     fprintf (fs, "%s\n", word);
     fclose (fs);

     return (EXIT_SUCCESS);
   }

   jib:~/eg/c > gcc a.c

   jib:~/eg/c > ./a.out created zero && cat created
   zero

   jib:~/eg/c > ./a.out created one && cat created
   one

   jib:~/eg/c > ./a.out created two && cat created
   two

   jib:~/eg/c > ./a.out created forty-two && cat created
   forty-two

if i am reading the man pages corectly this is how is SHOULD work everywhere -
once you've truncated the file and rewound append mode is essentially as in "w"
mode. perhaps this breaks down if you start doing random seeks - but it can be
made to work it seems.

regards.

-a

-- 
===============================================================================
| EMAIL   :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE   :: 303.497.6469
| When you do something, you should burn yourself completely, like a good
| bonfire, leaving no trace of yourself.  --Shunryu Suzuki
===============================================================================


Relevant Pages

  • Re: California city help
    ... Hmm. ... I knew I was lacking in cultural knowledge <running ... I did after reading your post! ... Cab-Merlot sure doesn't feel good via ...
    (sci.med.transcription)
  • Re: Orphan Star
    ... Sea Wasp wrote: ... > Ted Nolan wrote: ... >>>the end of my reading for that series. ... >> Hmm? ...
    (rec.arts.sf.written)
  • Re: Orphan Star
    ... Sea Wasp wrote: ... > Ted Nolan wrote: ... >>>the end of my reading for that series. ... >> Hmm? ...
    (rec.arts.sf.written)
  • Re: Javascript: is it possible to pass a parameter to a function inside a document write?
    ... Using a pointed stick and pebbles, ... Hmm, I was reading the OP as something else, ie. how to dynamically add ... Prev by Date: ...
    (alt.html)
  • Re: native interface
    ... Tom Anderson wrote: ... Probably true, but JNI really isn't that complex, so i think it's possible to pick up in an hour or two of reading the docs. ... If the book is good then it may be worth the price. ...
    (comp.lang.java.programmer)