[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Google
  Web www.spinics.net

Re: Fgets is not working on linux/gcc3.2.1



Michael Schwendt <ms0301rh@xxxxxxxx> writes:

> On Thu, 10 Apr 2003 18:03:02 +0530, Ajay Bansal wrote:
> 
> > Problem was not that I _have_ to read a file somehow. Problem was.. How
> > do read using fgets.  & why it bbehaves differently on Solaris/Linux
> > with same compiler. 
> > 
> > If I open the file with "a+", the on Solaris file pointer is at the
> > beginning of the file whereas on Linux it is at the end of the file. Why
> > is this difference???
> 
> 'a' = append, but appending makes sense only at the end of a file.
> 'a' without positioning the file pointer at the end of the file
> would be like 'w' (writing) and would NOT append, but overwrite
> (truncate) the file. 'a+' is like 'a' but also gives 'r'
> read-access.
> 
> What does the manual on Solaris say about 'a' and 'a+'?

Here's what the (Draft) C99 standard has to say about it:

      7.19.5.3  The fopen function

       Synopsis

       [#1]

               #include <stdio.h>
               FILE *fopen(const char * filename,
                       const char * mode);

       Description

       [#2]  The  fopen  function  opens the file whose name is the
       string pointed to by filename, and associates a stream  with
       it.

       [#3] The argument mode points to a string.  If the string is
       one of the following, the file  is  open  in  the  indicated
       mode.  Otherwise, the behavior is undefined.212)




       WG14/N843    Committee Draft  --  August 3, 1998         305


       r               open text file for reading
       w               truncate to zero length or create text file for
                       writing
       a               append; open or create text file for writing at
                       end-of-file
       rb              open binary file for reading
       wb              truncate to zero length or create binary file for
                       writing
       ab              append; open or create binary file for writing at
                       end-of-file
       r+              open text file for update (reading and writing)
       w+              truncate to zero length or create text file for update
       a+              append; open or create text file for update, writing
                       at end-of-file
       r+b or rb+      open binary file for update (reading and writing)
       w+b or wb+      truncate to zero length or create binary file for
                       update
       a+b or ab+      append; open or create binary file for update, writing
                       at end-of-file

       [#4]  Opening  a  file  with  read  mode  ('r'  as the first
       character in the mode argument) fails if the file  does  not
       exist or cannot be read.

       [#5]  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 the fseek  function.   In
       some implementations, opening a binary file with append mode
       ('b' as the second or third character in the above  list  of
       mode  argument  values)  may  initially  position  the  file
       position indicator for  the  stream  beyond  the  last  data
       written, because of null character padding.

       [#6]  When  a  file  is  opened with update mode ('+' as the
       second or third character in the above list of mode argument
       values),  both  input  and  output  may  be performed on the
       associated stream.  However, output shall  not  be  directly
       followed  by input without an intervening call to the fflush
       function or to a file positioning function (fseek,  fsetpos,
       or  rewind),  and  input  shall  not be directly followed by
       output without an intervening call  to  a  file  positioning
       function, unless the input operation encounters end-of-file.
       Opening (or creating) a  text  file  with  update  mode  may
       instead   open   (or   create)   a  binary  stream  in  some
       implementations.

       [#7] When opened, a stream is fully buffered if and only  if
       it  can be determined not to refer to an interactive device.
       The error and end-of-file  indicators  for  the  stream  are
       cleared.

       Returns


       ____________________

       212If the string begins with one of the above sequences, the
          implementation  might  choose  to  ignore  the  remaining
          characters,  or  it  might  use  them to select different
          kinds of a file (some of which might not conform  to  the
          properties in 7.19.2).

       7.19.5.3                   Library                  7.19.5.3




       306          Committee Draft  --  August 3, 1998   WG14/N843


       [#8]  The  fopen  function  returns  a pointer to the object
       controlling the stream.  If the open operation fails,  fopen
       returns a null pointer.

IOW, it doesn't say.  It says only that writes are to the end of the file.
It says nothing about reads.

> > And if some code is already written using fgets on solaris, this
> > difference makes it non-portable!!!!!
> 
> Remains to be seen whether the API is non-portable or whether the
> source code is.

The source code isn't portable.  Portable code must use fseek(), fsetpos(),
or rewind() to position the file for reading.

-- 
tim writer <tim@xxxxxxxxxxx>                                  starnix inc.
tollfree: 1-87-pro-linux                        thornhill, ontario, canada
http://www.starnix.com              professional linux services & products



_______________________________________________
Redhat-devel-list mailing list
Redhat-devel-list@xxxxxxxxxx
https://listman.redhat.com/mailman/listinfo/redhat-devel-list

[Kernel Newbies]     [Red Hat General]     [Fedora]     [Red Hat Install]     [Linux Kernel Development]     [Yosemite News]

Powered by Linux