H&S fread/fwrite

A

arnuld

This is from section 15.13 of H&S 5/e:

The functions fread and fwrite perform input and output, respectively,
to binary files.


Now there is no information on what fucntions should be used to read
text files. I know fread works butu as authors said, it is designed
for reading binary files, whereas I am looking for a function to read
text files. Any comments ?
 
M

mark.bluemel

This is from  section 15.13 of H&S 5/e:

The functions fread and fwrite perform input and output, respectively,
to binary files.

Now there is no information on what fucntions should be used to read
text files.  

fgets() and scanf() are obvious choices, I suppose.
I know fread works butu as authors said, it is designed
for reading binary files, whereas I am looking for a function to read
text files. Any comments ?

What are you trying to read from the text files? fread() is for a very
specific type of input, which to my mind only seems to be relevant to
binary files. There's no obvious text file equivalent.
 
M

Martin Ambuhl

arnuld said:
This is from section 15.13 of H&S 5/e:

The functions fread and fwrite perform input and output, respectively,
to binary files.


Now there is no information on what fucntions should be used to read
text files.

Did you just skip over sections 15.1-15.12, some 46 pages on how to read
and write text files?
I know fread works butu as authors said, it is designed
for reading binary files, whereas I am looking for a function to read
text files. Any comments ?

Since every text file is a binary file, you should be able to work this
out for yourself. fread() and fwrite() are for i/o in which you don't
want something translating any of what you think the data is into
something else. If you had read page 363, the beginning of chapter 15,
you would know that how end-of-line character sequences are handled is
what divide "text" files from "binary" files. Since you can always open
so-called text files in binary mode (and all a very large class of
operating systems there is, in fact, no difference), you obviously have
no problem to solve.
 
R

Richard Tobin

arnuld said:
The functions fread and fwrite perform input and output, respectively,
to binary files.

Now there is no information on what fucntions should be used to read
text files. I know fread works butu as authors said, it is designed
for reading binary files, whereas I am looking for a function to read
text files. Any comments ?

fread() and fwrite() are perfectly usable with text files, if what you
want to do is just read or write a number of characters. Remember that
they are implemented as if by calling fgetc() or fputc().

The reason that they are often *not* used with text files is that
they don't divide the input up into lines or anything else, which
is usually what you want with a text file. But if all you want to
do is, say, copy a file then they are the right tools for the job
regardless of whether the file is text or binary.

If you want lines, try fgets() and fputs(). If you want smaller units
such as words or numbers, fscanf() and fprintf() are useful. If you
need fine control (at the probably expense of greater overhead), use
getc() and putc().

-- Richard
 
L

lawrence.jones

Martin Ambuhl said:
Since every text file is a binary file

While that is true on many systems, it is not universally true.
Since you can always open
so-called text files in binary mode

That is also not universally true. There are systems where trying to
open a text file in binary mode will fail. There are other systems
where the open will succeed but the data you read from the file will be
much different than what you would expect.
 
K

Keith Thompson

Martin Ambuhl said:
arnuld wrote: [...]
I know fread works butu as authors said, it is designed
for reading binary files, whereas I am looking for a function to read
text files. Any comments ?

Since every text file is a binary file, you should be able to work
this out for yourself. fread() and fwrite() are for i/o in which you
don't want something translating any of what you think the data is
into something else. If you had read page 363, the beginning of
chapter 15, you would know that how end-of-line character sequences
are handled is what divide "text" files from "binary" files. Since
you can always open so-called text files in binary mode (and all a
very large class of operating systems there is, in fact, no
difference), you obviously have no problem to solve.

I don't think your assertion (that every text file is a binary file)
is supported by the standard, nor is your assertion that the handling
of end-of-line sequences is the only difference.

On most of the systems most people use these days (Unix, MS-DOS,
and their derivatives, including Linux, MacOS, and Windows), all
files are sequences of bytes, and text files are differentiated
by end-of-line handling and end-of-file handling (in a DOS/Windows
text file, control-Z is an end-of-file marker). On these systems,
the difference is in how the contents are interpreted.

On some systems, however, the type of a file is part of the
information maintained by the file system. It's entirely possible
to have a conforming C implementation where trying to open a text
file in binary mode, or a binary file in text mode, will always fail.

to quote anything I've written in this article without the usual
attribution line is granted if and only if this paragraph is quoted
*in full*. If you use an attribution line, feel free to trim.
Martin, I think you could solve all your claimed problems by adding
the word "allegedly" or something similar to your attribution lines.
 
R

Richard Bos

to quote anything I've written in this article without the usual
attribution line is granted if and only if this paragraph is quoted
*in full*. If you use an attribution line, feel free to trim.
Martin, I think you could solve all your claimed problems by adding
the word "allegedly" or something similar to your attribution lines.

Huh? As you can see above, Martin _did_ use an attribution line in the
post you quoted. Why this paragraph _now_?

Richard
 
K

Keith Thompson

Huh? As you can see above, Martin _did_ use an attribution line in the
post you quoted. Why this paragraph _now_?

Temporary stupidity on my part. I (a) didn't notice the attribution
line, and (b) forgot that it's not Martin Ambuhl who snips attribution
lines, it's Gordon Burditt.

Martin, I apologize for the mixup. Please disgregard the quoted
paragraph.
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top