H&S fread/fwrite

Discussion in 'C Programming' started by arnuld, Mar 18, 2009.

  1. arnuld

    arnuld Guest

    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 ?
    arnuld, Mar 18, 2009
    #1
    1. Advertising

  2. arnuld

    Guest

    Re: H&S fread/fwrite

    On 18 Mar, 11:20, 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.  


    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.
    , Mar 18, 2009
    #2
    1. Advertising

  3. 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.
    Martin Ambuhl, Mar 18, 2009
    #3
  4. In article <>,
    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.
    Richard Tobin, Mar 18, 2009
    #4
  5. arnuld

    Guest

    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
    , Mar 18, 2009
    #5
  6. 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, <>, 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) <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"
    Keith Thompson, Mar 18, 2009
    #6
  7. arnuld

    Richard Bos Guest

    Keith Thompson <> wrote:

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


    > I, Keith Thompson, <>, 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
    Richard Bos, Mar 19, 2009
    #7
  8. (Richard Bos) writes:
    > Keith Thompson <> wrote:
    >> Martin Ambuhl <> writes:
    >> > arnuld wrote:

    >
    >> I, Keith Thompson, <>, 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) <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"
    Keith Thompson, Mar 19, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brady

    problem using fread, fwrite, and fsetpos

    Brady, Jul 17, 2003, in forum: C Programming
    Replies:
    8
    Views:
    925
    Dave Thompson
    Jul 21, 2003
  2. Guest

    fread / fwrite error

    Guest, Aug 1, 2003, in forum: C Programming
    Replies:
    9
    Views:
    1,018
    Emmanuel Delahaye
    Aug 7, 2003
  3. CBFalconer

    Re: fread/fwrite Bits

    CBFalconer, Jan 3, 2004, in forum: C Programming
    Replies:
    0
    Views:
    838
    CBFalconer
    Jan 3, 2004
  4. mazsx
    Replies:
    2
    Views:
    852
    Toni Uusitalo
    Nov 11, 2005
  5. fread fwrite struct

    , Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    438
    Michael Mair
    Mar 31, 2006
Loading...

Share This Page