473,396 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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...
Nov 13 '05 #1
5 10790
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
Nov 13 '05 #2
fr***********@yahoo.com (Trying_Harder) wrote in message news:<b0**************************@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...

Nov 13 '05 #3
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.

Nov 13 '05 #4
In article <43**************************@posting.google.com >
John Bode <jo*******@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 (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.
Nov 13 '05 #5
Chris Torek <no****@elf.eng.bsdi.com> wrote in message news:<bj**********@elf.eng.bsdi.com>...
In article <43**************************@posting.google.com >
John Bode <jo*******@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.
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Diego | last post by:
Hi, Using gcc 2.96 This message was suggested by a thread started by Knak on 21/03/04 The question is: When I run the following code, if I want to introduce a second pile of data, the...
8
by: Magix | last post by:
Hi, I'm not too sure on reading specify string with the first < > from file stream,and make the file pointer go to next line, with fgets. or any better idea. E.g let say text.txt has...
7
by: Emerson | last post by:
Hi all: my problem is in the comments of the code . would u please give me some clue what 's the problem? what u in advance. /*******************************/ #include "stdio.h" #include...
6
by: AMT2K5 | last post by:
Hello. I have a file (for a school assignment) with the following format and delimiter format. Each record in the file has the following format: 123423454567987,29873,James,Harry,St....
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
3
by: FireHead | last post by:
Hello comp.lang.c Couple of years ago I created a topic as to how I can use the function fgets so that I can read a line of text from a source of data. After a long time I have finally got all...
6
by: Gert Kok | last post by:
When fgets() reads a string that ands with <<<EOT, the string is truncated after << and a lot of following input is discarded. When <<<EOT is changed to <<< EOT, behaviour is as I expect it. ...
6
by: DrSchwartz | last post by:
Hi all, I'm completely stuck.. I have a program which reads integers (there are 65536 integers) from one file, and adds them into the Bloom filter. And then it reads another file (with a larger set...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.