ideas on stream input

Discussion in 'C Programming' started by Bill Cunningham, Dec 7, 2011.

  1. I want to read formatted text from a text file into a function. Would I
    use fscanf or fgetc. For example, I have a text file called "num" that says
    the following:

    1 12.35
    2 11.68

    I want to read this text, stored it and pass it as parameters to other
    functions. If I used fscanf I have no idea how to use the format specifiers
    since I don't really use the scanf family. I usually use fgets to stdin for
    input and puts for writing. But this is to a file stream. I hope I'm making
    sense. Someone help me out with how reading with fscanf works. That's a new
    one on me.

    Bill
    Bill Cunningham, Dec 7, 2011
    #1
    1. Advertising

  2. "Bill Cunningham" <> writes:

    > I want to read formatted text from a text file into a function. Would I
    > use fscanf or fgetc. For example, I have a text file called "num" that says
    > the following:
    >
    > 1 12.35
    > 2 11.68
    >
    > I want to read this text, stored it and pass it as parameters to other
    > functions. If I used fscanf I have no idea how to use the format specifiers
    > since I don't really use the scanf family.


    You used to. In 2002 you wrote "I used to use scanf() all the time" and
    in 2008 I answered a question from you about fscanf formats.

    <snip>
    --
    Ben.
    Ben Bacarisse, Dec 7, 2011
    #2
    1. Advertising

  3. Ben Bacarisse wrote:
    > "Bill Cunningham" <> writes:
    >
    >> I want to read formatted text from a text file into a function.
    >> Would I use fscanf or fgetc. For example, I have a text file called
    >> "num" that says the following:
    >>
    >> 1 12.35
    >> 2 11.68
    >>
    >> I want to read this text, stored it and pass it as parameters to
    >> other functions. If I used fscanf I have no idea how to use the
    >> format specifiers since I don't really use the scanf family.

    >
    > You used to. In 2002 you wrote "I used to use scanf() all the time"
    > and in 2008 I answered a question from you about fscanf formats.
    >
    > <snip>


    I'll check and see what I can find.

    Bill
    Bill Cunningham, Dec 7, 2011
    #3
  4. Ben Bacarisse wrote:
    > "Bill Cunningham" <> writes:
    >
    >> I want to read formatted text from a text file into a function.
    >> Would I use fscanf or fgetc. For example, I have a text file called
    >> "num" that says the following:
    >>
    >> 1 12.35
    >> 2 11.68
    >>
    >> I want to read this text, stored it and pass it as parameters to
    >> other functions. If I used fscanf I have no idea how to use the
    >> format specifiers since I don't really use the scanf family.

    >
    > You used to. In 2002 you wrote "I used to use scanf() all the time"
    > and in 2008 I answered a question from you about fscanf formats.
    >
    > <snip>


    I did use scanf for a while until fgets. That was a long time ago.

    Bill
    Bill Cunningham, Dec 7, 2011
    #4
  5. On Dec 7, 2:14 am, "Bill Cunningham" <> wrote:

    >     I want to read formatted text from a text file into a function.


    ITYM "...into an array"

    > Would I
    > use fscanf or fgetc.


    dunno what you'd do. But I'd use fgets() followed by sscanf().
    fscanf() is tricky to write the error handling and fgetc() you're
    going to have to parse the input yourself. Another option for
    converting numbers is strtol() et al.

    > For example, I have a text file called "num" that says
    > the following:
    >
    > 1 12.35
    > 2 11.68
    >
    > I want to read this text, stored it and pass it as parameters to other
    > functions. If I used fscanf I have no idea how to use the format specifiers
    > since I don't really use the scanf family.


    RTFM

    > I usually use fgets to stdin for
    > input and puts for writing. But this is to a file stream.


    the "f" in fgets() stands for "file".

    > I hope I'm making
    > sense. Someone help me out with how reading with fscanf works. That's a new
    > one on me.


    you too idle to open a book? have you heard of the internet?
    Nick Keighley, Dec 8, 2011
    #5
  6. Bill Cunningham

    Rui Maciel Guest

    Bill Cunningham wrote:

    > I want to read formatted text from a text file into a function. Would
    > I
    > use fscanf or fgetc. For example, I have a text file called "num" that
    > says the following:
    >
    > 1 12.35
    > 2 11.68
    >
    > I want to read this text, stored it and pass it as parameters to other
    > functions.


    Don't rely on neither. Develop a proper parser and you will end up with a
    solution which is far better, far safer, far more flexible and far more
    powerful. It will also let you rely on a syntax which is far easier to read
    and understand.


    Rui Maciel
    Rui Maciel, Dec 8, 2011
    #6
  7. superpollo wrote:

    > could you please expand this a little'?
    >
    > bye


    Yes. I will write some code and post a little later. Then all can see
    what I'm trying to do.

    Bill
    Bill Cunningham, Dec 9, 2011
    #7
  8. Rui Maciel wrote:

    > Don't rely on neither. Develop a proper parser and you will end up
    > with a solution which is far better, far safer, far more flexible and
    > far more powerful. It will also let you rely on a syntax which is
    > far easier to read and understand.


    Sounds great. The thing is I don't know how to write a parser and have
    looked online for help. When I have some code together I will post
    something.

    Bill
    Bill Cunningham, Dec 9, 2011
    #8
  9. Nick Keighley wrote:

    [snip]

    > the "f" in fgets() stands for "file".


    scanf has buffer problems. I use this code...

    fgets(variable,sizeof (int),stdin);

    >> I hope I'm making
    >> sense. Someone help me out with how reading with fscanf works.
    >> That's a new one on me.

    >
    > you too idle to open a book? have you heard of the internet?
    Bill Cunningham, Dec 9, 2011
    #9
  10. On Dec 8, 5:18 pm, Nick Keighley <>
    wrote:
    > On Dec 7, 2:14 am, "Bill Cunningham" <> wrote:
    > > I hope I'm making
    > > sense. Someone help me out with how reading with fscanf works. That's anew
    > > one on me.

    >
    > you too idle to open a book? have you heard of the internet?


    will you still ask the question if bill posted his posts by google
    groups?

    and i can't understand why bill can't master C even when he himself is
    a native english speaker.
    lovecreatesbeauty, Dec 9, 2011
    #10
  11. On Thu, 8 Dec 2011 21:28:37 -0500, "Bill Cunningham"
    <> wrote:

    >Nick Keighley wrote:
    >
    >[snip]
    >
    >> the "f" in fgets() stands for "file".

    >
    > scanf has buffer problems. I use this code...
    >
    >fgets(variable,sizeof (int),stdin);


    It wouldn't surprise me at all if you actually used code like this.

    However, for novices following this thread, be aware that this code
    can never produce the correct results.

    --
    Remove del for email
    Barry Schwarz, Dec 9, 2011
    #11
  12. On Thu, 8 Dec 2011 21:26:08 -0500, "Bill Cunningham"
    <> wrote:

    >superpollo wrote:
    >
    >> could you please expand this a little'?
    >>
    >> bye

    >
    > Yes. I will write some code and post a little later. Then all can see
    >what I'm trying to do.


    If you want us to see what you are trying to do, describe it in
    English. Don't inundate us with your wild guesses at butchered code.

    --
    Remove del for email
    Barry Schwarz, Dec 9, 2011
    #12
  13. On Dec 9, 2:28 am, "Bill Cunningham" <> wrote:
    > Nick Keighley wrote:
    >
    > [snip]
    >
    > > the "f" in fgets() stands for "file".

    >
    >     scanf has buffer problems. I use this code...


    note that I said use sscanf(). That has two Ss on the front

    > fgets(variable,sizeof (int),stdin);


    what if your number has more than say 3 characters in it? Will your
    code read 1000 correctly? [the answer is "no").


    > >> I hope I'm making
    > >> sense. Someone help me out with how reading with fscanf works.
    > >> That's a new one on me.

    >
    > > you too idle to open a book? have you heard of the internet?
    Nick Keighley, Dec 9, 2011
    #13
  14. On Tuesday, December 6, 2011 6:14:48 PM UTC-8, Bill Cunningham wrote:
    > I want to read formatted text from a text file into a function. Would I
    > use fscanf or fgetc. For example, I have a text file called "num" that says
    > the following:
    >
    > 1 12.35
    > 2 11.68
    >
    > I want to read this text, stored it and pass it as parameters to other
    > functions. If I used fscanf I have no idea how to use the format specifiers
    > since I don't really use the scanf family. I usually use fgets to stdin for
    > input and puts for writing. But this is to a file stream. I hope I'm making
    > sense. Someone help me out with how reading with fscanf works. That's a new
    > one on me.


    Koko ni Kore wa ikimasu....

    Would you describe the format of the file? Is it fixed field, whitespace delimited, or what? Might there be commas? What significance would they have?(do separate fields or are they just for readability of the numbers?) Are there two fields per line or might there be more?

    Different methods apply depending upon the answers to those questions.

    If all of the lines in the file look like what you have shown, you could use a code fragment soething like this (ignoring errors):

    int seq;
    float val;
    FILE * f;
    int num_args;

    if (!(f = fopen ("num", "r"))
    giveup;
    while (!feof (f)) do
    {
    num_args = fscanf (f, "%d %5.2f\n", & seq, & val);
    if (num_args < 2)
    giveup;
    /* Do something with input */
    }
    Michael Angelo Ravera, Dec 9, 2011
    #14
  15. Michael Angelo Ravera <> writes:

    > On Tuesday, December 6, 2011 6:14:48 PM UTC-8, Bill Cunningham wrote:

    <snip>
    >> For example, I have a text file called "num" that says
    >> the following:
    >>
    >> 1 12.35
    >> 2 11.68

    <snip>
    > If all of the lines in the file look like what you have shown, you could use a code fragment soething like this (ignoring errors):
    >
    > int seq;
    > float val;
    > FILE * f;
    > int num_args;
    >
    > if (!(f = fopen ("num", "r"))
    > giveup;
    > while (!feof (f)) do
    > {
    > num_args = fscanf (f, "%d %5.2f\n", & seq, & val);
    > if (num_args < 2)
    > giveup;
    > /* Do something with input */
    > }


    That's a little un-C-like. Because of the way feof works, it's usually
    neater write the input operation in the loop condition:

    while (fscanf(f, "%d %f", &seq, &val) == 2) {
    /* Do something with input */
    }

    I think it's simpler and more natural (in particular how it handles
    end-of-file) but these are minor points. The big win for me is that it
    emphasises the normal flow -- what should happen is explicit rather than
    being the result of two negatives (!feof(f) and not num_args < 2).

    If you need to know what's gone wrong, you can test feof (and/or ferror)
    after the loop.

    Two details: %5.2f is not a valid scanf format and the \n at the end of
    the format adds nothing. It might, though, suggest a false security to
    those less well versed in C, so I'd skip it altogether.

    --
    Ben.
    Ben Bacarisse, Dec 9, 2011
    #15
  16. Bill Cunningham

    Jorgen Grahn Guest

    On Thu, 2011-12-08, superpollo wrote:
    > Rui Maciel ha scritto:
    >> Bill Cunningham wrote:
    >>
    >>> I want to read formatted text from a text file into a function. Would
    >>> I
    >>> use fscanf or fgetc. For example, I have a text file called "num" that
    >>> says the following:
    >>>
    >>> 1 12.35
    >>> 2 11.68
    >>>
    >>> I want to read this text, stored it and pass it as parameters to other
    >>> functions.

    >>
    >> Don't rely on neither. Develop a proper parser

    >
    > could you please expand this a little'?


    First, quote what he wrote properly:

    Rui:
    >> Don't rely on neither. Develop a proper parser and you will end up
    >> with a solution which is far better, far safer, far more flexible and
    >> far more powerful. It will also let you rely on a syntax which is far
    >> easier to read and understand.


    I am not him, but I would:

    - Define my input language. An example is not a definition.

    Is my language line-oriented with positive integer, space,
    floating-point value, end of line ? Or would I want to accept more
    space around the numbers? Would I want to allow empty lines and
    comments?

    - Then I'd follow the Perl idiom for writing the parser, but without
    the help of regexes.
    - read a line at a time
    - just whitespace, or a single-line comment #foo? Skip it.
    - see if I can find two non-blank parts of the line using isspace()
    - on the first, run strtol() or strtoul() and deal with any errors,
    including overflow
    - on the second, run strtod() similarly

    - make sure that in the end the caller can tell the difference between
    - no input
    - good input
    - some good input, then syntax error at line N

    When you've done this once or twice, it's not that hard.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Dec 10, 2011
    #16
  17. Bill Cunningham

    Philip Lantz Guest

    On Thu, 08 Dec 2011, Barry Schwarz wrote:

    > On Thu, 8 Dec 2011, Bill Cunningham wrote:
    >> fgets(variable,sizeof (int),stdin);

    >
    > However, for novices following this thread, be aware that this code
    > can never produce the correct results.


    Never is such a strong word. What if it were preceded by
    char variable[sizeof (int)];

    :)
    Philip Lantz, Dec 10, 2011
    #17
  18. Jorgen Grahn wrote:
    > On Thu, 2011-12-08, superpollo wrote:
    >> Rui Maciel ha scritto:
    >>> Bill Cunningham wrote:
    >>>
    >>>> I want to read formatted text from a text file into a
    >>>> function. Would I
    >>>> use fscanf or fgetc. For example, I have a text file called "num"
    >>>> that says the following:
    >>>>
    >>>> 1 12.35
    >>>> 2 11.68
    >>>>
    >>>> I want to read this text, stored it and pass it as parameters to
    >>>> other functions.
    >>>
    >>> Don't rely on neither. Develop a proper parser

    >>
    >> could you please expand this a little'?

    >
    > First, quote what he wrote properly:
    >
    > Rui:
    >>> Don't rely on neither. Develop a proper parser and you will end up
    >>> with a solution which is far better, far safer, far more flexible
    >>> and far more powerful. It will also let you rely on a syntax which
    >>> is far easier to read and understand.

    >
    > I am not him, but I would:
    >
    > - Define my input language. An example is not a definition.
    >
    > Is my language line-oriented with positive integer, space,
    > floating-point value, end of line ? Or would I want to accept more
    > space around the numbers? Would I want to allow empty lines and
    > comments?
    >
    > - Then I'd follow the Perl idiom for writing the parser, but without
    > the help of regexes.
    > - read a line at a time
    > - just whitespace, or a single-line comment #foo? Skip it.
    > - see if I can find two non-blank parts of the line using isspace()
    > - on the first, run strtol() or strtoul() and deal with any errors,
    > including overflow
    > - on the second, run strtod() similarly
    >
    > - make sure that in the end the caller can tell the difference between
    > - no input
    > - good input
    > - some good input, then syntax error at line N
    >
    > When you've done this once or twice, it's not that hard.
    >
    > /Jorgen


    I want at text file in this format.

    1 15.36 120912

    1 is line one, 15.36 a securities price, 120912 or I guess it should be 11
    is the date.

    I am thinking fscanf(fp,"%u%.2f%d\n",&a,&b,&c);

    void *tread(void *pointer) //parameter

    The function would reach into the text file via a pointer and retreive any
    of these 3 fields of data on successive calls and return the void * as a
    parameter to pass to another function that will accept the void* as a
    parameter. isspace() I am thinking needs to be in there somewhere too.

    If this isn't clear please say so. I'm going to try to piece something
    together today.

    Bill
    Bill Cunningham, Dec 10, 2011
    #18
  19. "Bill Cunningham" <nospam@invalid> writes:
    <snip>
    > I want at text file in this format.
    >
    > 1 15.36 120912
    >
    > 1 is line one, 15.36 a securities price, 120912 or I guess it should be 11
    > is the date.


    Do you get to choose the file format? If so, you could pick format for
    the date this is much easier for C's date and time functions. For
    example

    1 15.36 2011 09 12

    > I am thinking fscanf(fp,"%u%.2f%d\n",&a,&b,&c);


    The \n does not help -- it almost certainly does not do what you think
    it does -- and %.2f is not a valid format. Why use %u for the line
    number and %d for the date?

    I'd use fscanf(fp, "%u %f %lu"). The spaces serve no function other
    than to make the format more readable. Dates are likely to exceed the
    minimum guaranteed range of "unsigned int" so using "unsigned long"
    seems like a better idea.

    > void *tread(void *pointer) //parameter
    >
    > The function would reach into the text file via a pointer and retreive any
    > of these 3 fields of data on successive calls and return the void * as a
    > parameter to pass to another function that will accept the void* as a
    > parameter. isspace() I am thinking needs to be in there somewhere too.
    >
    > If this isn't clear please say so. I'm going to try to piece something
    > together today.


    It's not clear to me, no. I am not sure what "reaching into" means, and
    I am puzzled by the term "retrieve any of these 3 fields of data on
    successive calls". It sounds like it gets to choose which of the fields
    it returns. Furthermore, the function prototype is not helpful. Using
    void * just obscures what it's doing and will make using the results
    harder.

    --
    Ben.
    Ben Bacarisse, Dec 10, 2011
    #19
  20. On Dec 9, 5:20 am, lovecreatesbeauty <>
    wrote:
    > On Dec 8, 5:18 pm, Nick Keighley <>
    > wrote:
    >
    > > On Dec 7, 2:14 am, "Bill Cunningham" <> wrote:
    > > > I hope I'm making
    > > > sense. Someone help me out with how reading with fscanf works. That'sa new
    > > > one on me.

    >
    > > [you're] too idle to open a book? have you heard of the internet?

    >
    > will you still ask the question if bill posted his posts by google
    > groups?


    why not? I do.

    > and i can't understand why bill can't master C even when he himself is
    > a native english speaker.


    what?
    Nick Keighley, Dec 10, 2011
    #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. Rasmusson, Lars
    Replies:
    1
    Views:
    754
    popov
    Apr 30, 2004
  2. Replies:
    9
    Views:
    628
    Alex Buell
    Apr 27, 2006
  3. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    452
    John Harrison
    Feb 17, 2007
  4. dolphin
    Replies:
    6
    Views:
    556
    Thomas Fritsch
    Mar 18, 2007
  5. Kashif Ur Rehman
    Replies:
    2
    Views:
    864
    Tom Hawtin
    May 17, 2007
Loading...

Share This Page