Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fgets,fopen, fclose

Reply
Thread Tools

fgets,fopen, fclose

 
 
Trying_Harder
Guest
Posts: n/a
 
      08-26-2003
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...
 
Reply With Quote
 
 
 
 
Josh Sebastian
Guest
Posts: n/a
 
      08-26-2003
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/re...dio.html#fopen

Josh
 
Reply With Quote
 
 
 
 
John Bode
Guest
Posts: n/a
 
      08-26-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Trying_Harder) wrote in message news:<(E-Mail Removed). com>...
> 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...

 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      08-27-2003
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.

 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      09-03-2003
In article <(E-Mail Removed) >
John Bode <(E-Mail Removed)> 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 (4039.22'N, 11150.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.
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      09-03-2003
Chris Torek <(E-Mail Removed)> wrote in message news:<bj3fpc$hsv$(E-Mail Removed)>...
> In article <(E-Mail Removed) >
> John Bode <(E-Mail Removed)> 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.
 
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
Segmentation Fault in fclose()...why? VB C++ 3 01-15-2005 04:26 PM
fclose() causing access violation in threadFunc Anoop Nair C++ 1 08-05-2004 06:57 PM
Problem with FCLOSE Marcus Jacobs C Programming 11 08-07-2003 10:15 AM
fclose(stdin); viza C Programming 0 07-19-2003 07:45 PM



Advertisments