stream functions

Discussion in 'C Programming' started by Bill Cunningham, Feb 7, 2012.

  1. Now I want to make sure I get this straight. fread and fwrite is for
    binary files and data. fgets, fgetc, ftell, fseek, and the f*pos functions
    are for textual data. What should be put in that fpos_t* type with f*pos?

    Bill
    Bill Cunningham, Feb 7, 2012
    #1
    1. Advertising

  2. Bill Cunningham

    John Gordon Guest

    In <4f319cb1$0$1049$> "Bill Cunningham" <> writes:

    > Now I want to make sure I get this straight. fread and fwrite is for
    > binary files and data. fgets, fgetc, ftell, fseek, and the f*pos functions
    > are for textual data.


    No, that's not right.

    fread and fwrite work on both binary and text data.

    fgets is for reading one line of text from a stream.

    fgetc is for reading one character from a stream.

    ftell and fseek are for getting and setting the position within a stream.

    The f*pos() functions handle streams which are too large for ftell/fseek,
    and they also have special code to handle multibyte stream content.

    Where do you come up with this stuff, Bill? Do you not have access to
    documentation?

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Feb 7, 2012
    #2
    1. Advertising

  3. John Gordon wrote:
    >
    > No, that's not right.
    >
    > fread and fwrite work on both binary and text data.
    >
    > fgets is for reading one line of text from a stream.
    >
    > fgetc is for reading one character from a stream.
    >
    > ftell and fseek are for getting and setting the position within a
    > stream.
    >
    > The f*pos() functions handle streams which are too large for
    > ftell/fseek,
    > and they also have special code to handle multibyte stream content.
    >
    > Where do you come up with this stuff, Bill? Do you not have access to
    > documentation?


    Yes I read the man pages. That's what it looked like to me. What you
    said about f*pos functions handling things too big, well...It wasn't in the
    man pages so it's new to me. I guess it would depend on what the sizeof that
    pointer to that fpos_t type was I guess. What are you fseeking in a text
    file for? I know text right? What if I was seeking for a number in a column
    of a text file written with nano? Say 10. How would I skip all the other
    10's in the file?

    Bill
    Bill Cunningham, Feb 7, 2012
    #3
  4. Bill Cunningham wrote:
    > John Gordon wrote:
    >>
    >> No, that's not right.
    >>
    >> fread and fwrite work on both binary and text data.
    >>
    >> fgets is for reading one line of text from a stream.
    >>
    >> fgetc is for reading one character from a stream.
    >>
    >> ftell and fseek are for getting and setting the position within a
    >> stream.

    [snip]

    So is ftell and fseek for binary files?

    Bill
    Bill Cunningham, Feb 7, 2012
    #4
  5. Bill Cunningham wrote:
    > [snip]
    >
    > So is ftell and fseek for binary files?
    >
    > Bill
    >
    >

    They are _all_ for binary files.
    Text files are a subset of them

    ....

    Except in windows, where newline-carriage-return conversion may be going
    on if you do not fopen() with "rb" or "wb"
    Johann Klammer, Feb 8, 2012
    #5
  6. Johann Klammer <1.net> writes:
    > Bill Cunningham wrote:
    >> [snip]
    >>
    >> So is ftell and fseek for binary files?
    >>
    >> Bill
    >>
    >>

    > They are _all_ for binary files.
    > Text files are a subset of them
    >
    > ...
    >
    > Except in windows, where newline-carriage-return conversion may be going
    > on if you do not fopen() with "rb" or "wb"


    And except for systems based on neither Unix nor MS-DOS/Windows, where
    the distinction between binary and text files might be even more
    significant. C's stdio is very deliberately designed to work on such
    systems.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 8, 2012
    #6
  7. Keith Thompson wrote:

    > And except for systems based on neither Unix nor MS-DOS/Windows, where
    > the distinction between binary and text files might be even more
    > significant. C's stdio is very deliberately designed to work on such
    > systems.


    What do you mean for example Keith? I know for portability to use "rb"
    and "wb" but is there even more distinction you say from binary and text
    files? I think Unix treats everything like a text file.

    Bill
    Bill Cunningham, Feb 8, 2012
    #7
  8. On Wed, 08 Feb 2012 02:39:35 +0100, Johann Klammer
    <1.net> wrote:

    >Bill Cunningham wrote:
    >> [snip]
    >>
    >> So is ftell and fseek for binary files?
    >>
    >> Bill
    >>
    >>

    >They are _all_ for binary files.
    >Text files are a subset of them
    >
    >...
    >
    >Except in windows, where newline-carriage-return conversion may be going
    >on if you do not fopen() with "rb" or "wb"


    Windows is not the only OS where the '\n' does not match the external
    representation of the file. For IBM's MVS and its descendents, the
    '\n' is usually not present in the file at all.

    --
    Remove del for email
    Barry Schwarz, Feb 8, 2012
    #8
  9. Bill Cunningham

    John Gordon Guest

    In <4f31b446$0$1075$> "Bill Cunningham" <> writes:

    > What if I was seeking for a number in a column of a text file


    That's not what fseek does. fseek goes to a specific location in a file,
    as defined by an offset from the beginning or end. It does not "seek" for
    specific content in the file.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Feb 8, 2012
    #9
  10. Bill Cunningham

    Nobody Guest

    On Tue, 07 Feb 2012 22:36:23 -0500, Bill Cunningham wrote:

    >> And except for systems based on neither Unix nor MS-DOS/Windows, where
    >> the distinction between binary and text files might be even more
    >> significant. C's stdio is very deliberately designed to work on such
    >> systems.

    >
    > What do you mean for example Keith? I know for portability to use "rb"
    > and "wb" but is there even more distinction you say from binary and text
    > files?


    fseek() and ftell() have some weirdness with files opened in text mode on
    platforms where text and binary modes differ. E.g. for Windows:

    http://msdn.microsoft.com/en-us/library/75yw9bf3(v=vs.71).aspx

    For streams opened in text mode, fseek has limited use, because
    carriage return–linefeed translations can cause fseek to produce
    unexpected results. The only fseek operations guaranteed to work on
    streams opened in text mode are:

    Seeking with an offset of 0 relative to any of the origin values.

    Seeking from the beginning of the file with an offset value
    returned from a call to ftell.

    Also in text mode, CTRL+Z is interpreted as an end-of-file character
    on input. In files opened for reading/writing, fopen and all related
    routines check for a CTRL+Z at the end of the file and remove it if
    possible. This is done because using fseek and ftell to move within a
    file that ends with a CTRL+Z may cause fseek to behave improperly near
    the end of the file.

    My system with a copy of the C99 standard isn't to hand, but I'm fairly
    sure that the standard says something similar, i.e. that fseek/ftell have
    well-defined behaviour when used on binary streams but a large amount of
    undefined behaviour when used on text streams.

    > I think Unix treats everything like a text file.


    Unix uses a bare LF ('\n') as its EOL indicator and doesn't have an EOF
    indicator, so there's no distinction between text and binary files on Unix
    (i.e. there's no difference between "r" and "rb", etc).
    Nobody, Feb 8, 2012
    #10
  11. John Gordon wrote:
    > In <4f31b446$0$1075$> "Bill Cunningham"
    > <> writes:
    >
    >> What if I was seeking for a number in a column of a text file

    >
    > That's not what fseek does. fseek goes to a specific location in a
    > file,
    > as defined by an offset from the beginning or end. It does not
    > "seek" for
    > specific content in the file.


    Ok so with a page of text, like a man page for example; scanning that
    text would best be done with what? scanf or fscanf?

    Bill
    Bill Cunningham, Feb 8, 2012
    #11
  12. Bill Cunningham

    John Gordon Guest

    In <4f32e0fa$0$1300$> "Bill Cunningham" <> writes:

    > Ok so with a page of text, like a man page for example; scanning that
    > text would best be done with what? scanf or fscanf?


    I'd use fgets to read the lines of text and store them in an array.

    After that, it depends how the information is formatted. If the text
    is arranged in columns, sscanf might be a good candidate for picking
    out individual pieces of information. Or if it's plain free-form text,
    strstr might be a better choice.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Feb 8, 2012
    #12
  13. John Gordon wrote:

    > I'd use fgets to read the lines of text and store them in an array.
    >
    > After that, it depends how the information is formatted. If the text
    > is arranged in columns, sscanf might be a good candidate for picking
    > out individual pieces of information. Or if it's plain free-form
    > text,
    > strstr might be a better choice.


    Ok so to scan the entire page and get results, fgets in a loop would be
    best then? The text file I want to write will have columns of data. I hope
    I'm clear. Loops is something I've never really caught onto in C. Especially
    loops within loops.

    Bill
    Bill Cunningham, Feb 8, 2012
    #13
  14. Bill Cunningham

    John Gordon Guest

    In <4f32f90d$0$32645$> "Bill Cunningham" <> writes:

    > Ok so to scan the entire page and get results, fgets in a loop would be
    > best then?


    To scan the page, you really need to do two things:

    1. Read the information and store it in some organized way.
    2. Inspect the stored information.

    fgets is good for #1. For #2, you'll need something like sscanf or strstr.

    > Loops is something I've never really caught onto in C.


    Loops are a basic concept in almost any programming language; perhaps
    you should brush up on them in C.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Feb 8, 2012
    #14
  15. "Bill Cunningham" <> schrieb im Newsbeitrag
    news:4f32f90d$0$32645$...

    ....

    > The text file I want to write will have columns of data.

    If the columns are e.g. padded with spaces, then you might do this:
    if (fgets(Buffer, sizeof(Buffer), fp))
    {
    *Value = 0;
    strncat(Value, Buffer + ValueColumn, sizeof(Value) - 1);
    }

    If the columns are e.g. delimited with tab, then you might do that:
    cp = strtok(Buffer, "\t");
    Column = 0;
    while (cp && Column < NUMCOLUMNS)
    {
    *Value[Column] = 0;
    strncat(Value[Column], cp, sizeof(Value[Column]) - 1);
    cp = strtok(NULL, "\t");
    Column ++;
    }
    ....
    Heinrich Wolf, Feb 9, 2012
    #15
  16. Heinrich Wolf wrote:

    > If the columns are e.g. padded with spaces, then you might do this:
    > if (fgets(Buffer, sizeof(Buffer), fp))
    > {
    > *Value = 0;
    > strncat(Value, Buffer + ValueColumn, sizeof(Value) - 1);
    > }
    >
    > If the columns are e.g. delimited with tab, then you might do that:
    > cp = strtok(Buffer, "\t");
    > Column = 0;
    > while (cp && Column < NUMCOLUMNS)
    > {
    > *Value[Column] = 0;
    > strncat(Value[Column], cp, sizeof(Value[Column]) - 1);
    > cp = strtok(NULL, "\t");
    > Column ++;
    > }
    > ...


    Thanks for the code example. I will keep it and study it. I recognize
    *Value to be a derefence but I'm still not exactly sure when to use those
    still at this time. My code is much much simpler.

    Bill
    Bill Cunningham, Feb 10, 2012
    #16
  17. Bill Cunningham

    Jorgen Grahn Guest

    On Mon, 2012-02-13, Gordon Burditt wrote:
    [attribution lost]
    >> I guess it would depend on what the sizeof that
    >> pointer to that fpos_t type was I guess. What are you fseeking in a text
    >> file for?

    >
    > It is very common for a program to fseek() to the end of a log file
    > (or sometimes a data file) to add messages to it. Using fopen(name,
    > "a") is not always useful as you might sometimes write to somewhere
    > other than the end.


    Explain, please. My manual says about "a":

    Open for appending (writing at end of file). The file is created
    if it does not exist. The stream is positioned at the end of the
    file.

    Someone else might have appended to the file after you fopen()ed it,
    but the same is true for fseek().

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Feb 13, 2012
    #17
  18. (Gordon Burditt) writes:
    > John Gordon wrote:
    >> Yes I read the man pages. That's what it looked like to me. What you
    >> said about f*pos functions handling things too big, well...It wasn't in the
    >> man pages so it's new to me.

    >
    > These functions are new to C99.


    No, fgetpos() and fsetpos() are in C90.

    [attribution line added]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 13, 2012
    #18
  19. On Feb 8, 11:16 pm, John Gordon <> wrote:
    > In <4f32f90d$0$32645$> "Bill Cunningham" <> writes:


    <snip>

    > > Loops is something I've never really caught onto in C.

    >
    > Loops are a basic concept in almost any programming language;


    I think some functional languages manage without them. Certainly Lisp,
    C++, Python programs can be written that don't use loops.

    > perhaps you should brush up on them in C.


    to put it mildly! Loops are absolutly fundamental to a language like C.
    Nick Keighley, Feb 13, 2012
    #19
  20. On Feb 8, 8:54 pm, "Bill Cunningham" <> wrote:

    <snip>

    >     Ok so with a page of text, like a man page for example; scanning that
    > text would best be done with what? scanf or fscanf?


    scanf() reads from stdin fscanf() reads froma file. What would you
    think?
    Nick Keighley, Feb 13, 2012
    #20
    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. Xiangliang Meng
    Replies:
    1
    Views:
    1,576
    Victor Bazarov
    Jun 21, 2004
  2. Rasmusson, Lars
    Replies:
    1
    Views:
    712
    popov
    Apr 30, 2004
  3. Replies:
    9
    Views:
    612
    Alex Buell
    Apr 27, 2006
  4. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    442
    John Harrison
    Feb 17, 2007
  5. dolphin
    Replies:
    6
    Views:
    544
    Thomas Fritsch
    Mar 18, 2007
Loading...

Share This Page