Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > H&S fread/fwrite

Reply
Thread Tools

H&S fread/fwrite

 
 
arnuld
Guest
Posts: n/a
 
      03-18-2009
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 ?
 
Reply With Quote
 
 
 
 
mark.bluemel@googlemail.com
Guest
Posts: n/a
 
      03-18-2009
On 18 Mar, 11:20, arnuld <arnuld.miz...@gmail.com> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-18-2009
arnuld wrote:
> 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.

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      03-18-2009
In article <206559c1-67b5-4c24-9075->,
arnuld <> wrote:

>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
--
Please remember to mention me / in tapes you leave behind.
 
Reply With Quote
 
lawrence.jones@siemens.com
Guest
Posts: n/a
 
      03-18-2009
Martin Ambuhl <> wrote:
>
> 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.
--
Larry Jones

We seem to be out of gun powder. -- Calvin
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-18-2009
Martin Ambuhl <> writes:
> 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.

I, Keith Thompson, <kst->, wrote the above. Permission
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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      03-19-2009
Keith Thompson <kst-> wrote:

> Martin Ambuhl <> writes:
> > arnuld wrote:


> I, Keith Thompson, <kst->, wrote the above. Permission
> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-19-2009
(Richard Bos) writes:
> Keith Thompson <kst-> wrote:
>> Martin Ambuhl <> writes:
>> > arnuld wrote:

>
>> I, Keith Thompson, <kst->, wrote the above. Permission
>> 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_?


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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
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




Advertisments