Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   fgets,fopen, fclose (http://www.velocityreviews.com/forums/t314948-fgets-fopen-fclose.html)

Trying_Harder 08-26-2003 12:25 AM

fgets,fopen, fclose
 
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...

Josh Sebastian 08-26-2003 12:34 AM

Re: fgets,fopen, fclose
 
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

John Bode 08-26-2003 05:16 PM

Re: fgets,fopen, fclose
 
frivolousdude@yahoo.com (Trying_Harder) wrote in message news:<b0430d9c.0308251625.75bb4283@posting.google. 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...


Lew Pitcher 08-27-2003 01:51 AM

Re: fgets,fopen, fclose
 
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.


Chris Torek 09-03-2003 01:22 AM

Re: fgets,fopen, fclose
 
In article <43618c0e.0308260916.38013af6@posting.google.com >
John Bode <john_bode@my-deja.com> 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.

John Bode 09-03-2003 05:15 PM

Re: fgets,fopen, fclose
 
Chris Torek <nospam@elf.eng.bsdi.com> wrote in message news:<bj3fpc$hsv$1@elf.eng.bsdi.com>...
> In article <43618c0e.0308260916.38013af6@posting.google.com >
> John Bode <john_bode@my-deja.com> 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.


All times are GMT. The time now is 02:59 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.