fgets,fopen, fclose

T

Trying_Harder

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...
 
J

Josh Sebastian

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
 
J

John Bode

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.
 
L

Lew Pitcher

Trying_Harder said:
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.
 
C

Chris Torek

John Bode said:
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.
 
J

John Bode

Chris Torek said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top