fgets,fopen, fclose

Discussion in 'C Programming' started by Trying_Harder, Aug 26, 2003.

  1. Generally we have function names briefly indicating their actions.
    In similar lines I expected `fopen' mean "File Open", fgets "File
    Get String" etc.

    I was told recently this wasn't the case though with the above
    mentioned functions. `fgets' meant "Function Get String" similar
    to `fputc' ("Function Put Character") or `fgetc' ( " Function Get
    Character " ) which are macros of functions `getc' and `putc'
    respectively.

    Can someone please confirm this?

    Also, do a+, r+, w+ mean the same? i.e. by defenition they all
    seem to update a file. Can someone please elaborate?

    p.s : I have referred to comp.lan.c FAQ's and didn't get clear
    answers.

    Thanks...
     
    Trying_Harder, Aug 26, 2003
    #1
    1. Advertising

  2. On Mon, 25 Aug 2003 17:25:43 -0700, Trying_Harder wrote:

    > Generally we have function names briefly indicating their actions.
    > In similar lines I expected `fopen' mean "File Open", fgets "File
    > Get String" etc.


    Yes.

    > I was told recently this wasn't the case though with the above
    > mentioned functions. `fgets' meant "Function Get String"


    No.

    Also, the f in printf, scanf, etc means "formatted" not "function".

    > Also, do a+, r+, w+ mean the same? i.e. by defenition they all
    > seem to update a file. Can someone please elaborate?


    No, but they do the same thing if the file doesn't exist (or is blank).
    See http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=stdio.html#fopen

    Josh
     
    Josh Sebastian, Aug 26, 2003
    #2
    1. Advertising

  3. Trying_Harder

    John Bode Guest

    (Trying_Harder) wrote in message news:<>...
    > Generally we have function names briefly indicating their actions.
    > In similar lines I expected `fopen' mean "File Open", fgets "File
    > Get String" etc.
    >


    This is generally true.

    > I was told recently this wasn't the case though with the above
    > mentioned functions. `fgets' meant "Function Get String" similar
    > to `fputc' ("Function Put Character") or `fgetc' ( " Function Get
    > Character " ) which are macros of functions `getc' and `putc'
    > respectively.
    >


    In these cases, it's pretty safe to assume the 'f' indicates file
    (technically stream) operations. I mean, c'mon, "function get
    string?" That's kind of nonsensical. You're not reading a string
    from a function, you're reading it from a stream (file).

    > Can someone please confirm this?
    >
    > Also, do a+, r+, w+ mean the same? i.e. by defenition they all
    > seem to update a file. Can someone please elaborate?


    r -- open an existing file for input only.
    r+ -- open an existing file for input *and* ouput, starting at the
    beginning of the file.

    w -- create a new file, or truncate an existing one, for output only.
    w+ -- create a new file, or truncate an existing one, for input and
    output.

    a -- create a new file, or append to an existing one, for output
    only.
    a+ -- create a new file, or append to an existing one, for input and
    output.

    Use r+ when you want to keep and edit existing records, then create
    and edit new records. Use w+ when you want to throw away all existing
    records, then create and edit new records. Use a+ when you want to
    preserve existing records, then create and edit new records.

    >
    > p.s : I have referred to comp.lan.c FAQ's and didn't get clear
    > answers.
    >
    > Thanks...
     
    John Bode, Aug 26, 2003
    #3
  4. Trying_Harder

    Lew Pitcher Guest

    Trying_Harder wrote:
    > Generally we have function names briefly indicating their actions.
    > In similar lines I expected `fopen' mean "File Open", fgets "File
    > Get String" etc.
    >
    > I was told recently this wasn't the case though with the above
    > mentioned functions. `fgets' meant "Function Get String" similar
    > to `fputc' ("Function Put Character") or `fgetc' ( " Function Get
    > Character " ) which are macros of functions `getc' and `putc'
    > respectively.


    IMO, fprintf -> File Print Formatted
    fopen -> File Open
    fgets -> File Get String
    fgetc -> File Get Char
    fputc -> File Put Char

    > Can someone please confirm this?


    The names are historical, and (outside of internal AT&T documentations)
    their derivation isn't well documented. However, it can be implied from
    the function and naming of comparable functions

    i.e.
    - printf() performs a formatted print
    scanf() performs a formatted scan
    thus the trailing 'f' of the name indicates that the function uses a
    format string

    - open() opens a file, returning an FD (admittedly OT here, as open() is
    not ANSI C, but of historical interest as the progenitor function
    for the C file open function
    fopen() opens a file, returning a FILE *
    close() closes a file using it's FD (again OT, but of similar
    historical interest as the progenitor function for the C file close)
    fclose() closes a file using it's FILE *
    thus, the leading 'f' of the name indicates that the function uses or
    returns a FILE *


    > Also, do a+, r+, w+ mean the same? i.e. by defenition they all
    > seem to update a file. Can someone please elaborate?


    Yes and no. The "+" indicates that the file will be opened for update,
    but the other character alters the start position of the update

    "r+" opens an existing, full, readable file for update, with initial
    position at start of file

    "w+" opens an empty (truncated if necessary) file for update, with
    initial position at start of file

    "a+" opens an existing full readable file or an empty new file for
    update with initial position at the end of the file.

    > p.s : I have referred to comp.lan.c FAQ's and didn't get clear
    > answers.




    --
    Lew Pitcher

    Master Codewright and JOAT-in-training
    Registered Linux User #112576 (http://counter.li.org/)
    Slackware - Because I know what I'm doing.
     
    Lew Pitcher, Aug 27, 2003
    #4
  5. Trying_Harder

    Chris Torek Guest

    In article <>
    John Bode <> writes:
    [a bunch of correct stuff, including]
    >a+ -- create a new file, or append to an existing one, for input and
    >output.
    >
    >Use r+ when you want to keep and edit existing records, then create
    >and edit new records. Use w+ when you want to throw away all existing
    >records, then create and edit new records. Use a+ when you want to
    >preserve existing records, then create and edit new records.


    Note that "a+" will force *all* writes to append, even if you
    fseek() first. This annoying "feature" was added in System V Unix,
    and the C Standards folks adopted it without, apparently, considering
    what happens if you want to "open, creating if needed but not
    overwriting, for reading and writing at any position chosen by the
    programmer".

    That mode -- "create if needed, but let me read and change anything
    anywhere" -- is simply missing.
    --
    In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://67.40.109.61/torek/index.html (for the moment)
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Sep 3, 2003
    #5
  6. Trying_Harder

    John Bode Guest

    Chris Torek <> wrote in message news:<bj3fpc$hsv$>...
    > In article <>
    > John Bode <> writes:
    > [a bunch of correct stuff, including]
    > >a+ -- create a new file, or append to an existing one, for input and
    > >output.
    > >
    > >Use r+ when you want to keep and edit existing records, then create
    > >and edit new records. Use w+ when you want to throw away all existing
    > >records, then create and edit new records. Use a+ when you want to
    > >preserve existing records, then create and edit new records.

    >
    > Note that "a+" will force *all* writes to append, even if you
    > fseek() first. This annoying "feature" was added in System V Unix,
    > and the C Standards folks adopted it without, apparently, considering
    > what happens if you want to "open, creating if needed but not
    > overwriting, for reading and writing at any position chosen by the
    > programmer".
    >
    > That mode -- "create if needed, but let me read and change anything
    > anywhere" -- is simply missing.


    I did not know that (tells you how often I open files a+). Thanks for
    that information.
     
    John Bode, Sep 3, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prama

    Re: Question about fopen, fput, fclose?

    prama, Aug 26, 2003, in forum: C Programming
    Replies:
    5
    Views:
    1,759
    Jack Klein
    Aug 28, 2003
  2. Peter Shaggy Haywood

    Re: Question about fopen, fput, fclose?

    Peter Shaggy Haywood, Aug 30, 2003, in forum: C Programming
    Replies:
    0
    Views:
    397
    Peter Shaggy Haywood
    Aug 30, 2003
  3. lihua
    Replies:
    19
    Views:
    935
    CBFalconer
    Jul 7, 2005
  4. David Mathog

    fclose then fopen equivalent for stdout?

    David Mathog, Oct 25, 2006, in forum: C Programming
    Replies:
    20
    Views:
    2,025
    Jordan Abel
    Oct 27, 2006
  5. kathy

    fopen and fclose?

    kathy, Feb 3, 2006, in forum: C++
    Replies:
    17
    Views:
    639
    Default User
    Feb 15, 2006
Loading...

Share This Page