fscanf

Discussion in 'C Programming' started by Bill Cunningham, Aug 18, 2008.

  1. I'm doing something wrong and all I know to do is turn to clc. I have a
    text file containing 2 doubles separated by a tab.

    ..26 0

    Is the text. I want to read the two double and printf them out. Here's my
    file.

    #include <stdio.h>

    int main() {
    FILE *fp;
    double x,y;
    fp=fopen("zo","r"); /*error checking out for brevity */
    fscanf(fp,"%.2f\t%.2f",&string);
    fclose(fp);
    printf("%.2f%.2f",x,y);
    }

    All I get is garbage that is contained in x and y. For whatever simple
    reason that is beyond me evidently I can't read and printf out to stdin from
    this text file. I don't think fread is really necessary.

    Bill
     
    Bill Cunningham, Aug 18, 2008
    #1
    1. Advertising

  2. Bill Cunningham

    Guest

    On Aug 18, 2:39 am, "Bill Cunningham" <> wrote:
    > I'm doing something wrong and all I know to do is turn to clc. I have a
    > text file containing 2 doubles separated by a tab.
    >
    > .26 0
    >
    > Is the text. I want to read the two double and printf them out. Here's my
    > file.
    >
    > #include <stdio.h>
    >
    > int main() {
    > FILE *fp;
    > double x,y;
    > fp=fopen("zo","r"); /*error checking out for brevity */
    > fscanf(fp,"%.2f\t%.2f",&string);
    > fclose(fp);
    > printf("%.2f%.2f",x,y);
    >
    > }
    >
    > All I get is garbage that is contained in x and y. For whatever simple
    > reason that is beyond me evidently I can't read and printf out to stdin from
    > this text file. I don't think fread is really necessary.


    You're a liar, Bill. That code does not compile, so it's impossible
    for you to get anything out of executing this program.
     
    , Aug 18, 2008
    #2
    1. Advertising

  3. On Sun, 17 Aug 2008 23:39:25 +0000, "Bill Cunningham" <>
    wrote:

    > I'm doing something wrong and all I know to do is turn to clc. I have a
    > text file containing 2 doubles separated by a tab.
    >
    > .26 0
    >
    > Is the text. I want to read the two double and printf them out. Here's
    > my file.
    >


    <snipped code>

    Please post the *actual* code that you used to compile and generate the
    executable that caused garbage to be printed. The code you posted will
    not compile.

    - Anand
     
    Anand Hariharan, Aug 18, 2008
    #3
  4. "Anand Hariharan" <> wrote in message
    news:g8ad99$v1a$...

    > Please post the *actual* code that you used to compile and generate the
    > executable that caused garbage to be printed. The code you posted will
    > not compile.


    Ok I did write this on the fly. I will look again at the actual code.

    Bill
     
    Bill Cunningham, Aug 18, 2008
    #4
  5. "Bill Cunningham" <> wrote in message
    news:yx3qk.131$w51.17@trnddc01...

    > Ok I did write this on the fly. I will look again at the actual code.
    >

    #include <stdio.h>

    int main() {
    FILE *fp;
    double x,y;
    fp=fopen("zo","a");
    fscanf(fp,"%.2f\t%.2f",&x,&y);
    fclose(fp);
    printf("%.2f\t%.2f",x,y);
    }

    Now this compiled for me with the results 0.00 and 4.87. Not the text
    from the file called "zo". The only real difference here is the text mode is
    append and not read.

    Bill
     
    Bill Cunningham, Aug 18, 2008
    #5
  6. Bill Cunningham

    Ian Collins Guest

    Bill Cunningham wrote:
    > "Bill Cunningham" <> wrote in message
    > news:yx3qk.131$w51.17@trnddc01...
    >
    >> Ok I did write this on the fly. I will look again at the actual code.
    >>

    > #include <stdio.h>
    >
    > int main() {
    > FILE *fp;
    > double x,y;
    > fp=fopen("zo","a");
    > fscanf(fp,"%.2f\t%.2f",&x,&y);
    > fclose(fp);
    > printf("%.2f\t%.2f",x,y);
    > }
    >
    > Now this compiled for me with the results 0.00 and 4.87. Not the text
    > from the file called "zo". The only real difference here is the text mode is
    > append and not read.
    >

    So what does your documentation well you about append mode?

    --
    Ian Collins.
     
    Ian Collins, Aug 18, 2008
    #6
  7. "Ian Collins" <> wrote in message
    news:...

    > So what does your documentation well you about append mode?
    >

    It just says append is a possible mode with a+ also which is append-read
    mode. Plus this book tells me that garbage about using t if I want for text
    mode. I know that's not portable stdc.
    http://www.cppreference.com/stdio/fopen.html

    Is my main online reference. I've been thinking my trouble is in fscanf. It
    might be in fopen's mode.

    Bill
     
    Bill Cunningham, Aug 18, 2008
    #7
  8. Bill Cunningham

    Ian Collins Guest

    Bill Cunningham wrote:
    > "Ian Collins" <> wrote in message
    > news:...
    >
    >> So what does your documentation well you about append mode?
    >>

    > It just says append is a possible mode with a+ also which is append-read
    > mode. Plus this book tells me that garbage about using t if I want for text
    > mode. I know that's not portable stdc.
    > http://www.cppreference.com/stdio/fopen.html
    >
    > Is my main online reference.


    Well it's a piss-poor one if that page is anything to go by. I suggest
    you look up the definition of "append"

    --
    Ian Collins.
     
    Ian Collins, Aug 18, 2008
    #8
  9. Bill Cunningham

    Paul Guest

    Bill Cunningham wrote:
    > "Ian Collins" <> wrote in message
    > news:...
    >
    >> So what does your documentation well you about append mode?
    >>

    > It just says append is a possible mode with a+ also which is append-read
    > mode. Plus this book tells me that garbage about using t if I want for text
    > mode. I know that's not portable stdc.
    > http://www.cppreference.com/stdio/fopen.html
    >
    > Is my main online reference. I've been thinking my trouble is in fscanf. It
    > might be in fopen's mode.
    >
    > Bill


    http://linux.die.net/man/3/fopen

    "The argument mode points to a string beginning with one of the following
    sequences (Additional characters may follow these sequences.):

    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. <---- yikes

    "

    http://linux.die.net/man/3/fscanf

    "Return Value

    These functions return the number of input items successfully matched and assigned,
    which can be fewer than provided for, or even zero in the event of an early matching
    failure.

    The value EOF is returned if the end of input is reached before either the first
    successful conversion or a matching failure occurs. EOF is also returned if a read
    error occurs, in which case the error indicator for the stream (see ferror(3)) is
    set, and errno is set indicate the error."

    Defensive programming, means checking the values returned by things like fscanf.
    Wouldn't you be curious, whether fscanf converted zero, one, or two items ? If the
    answer is not two, then X or Y could contain bogus information. And if an end of
    file was encountered, you'd probably want to know about that also. There are many
    possible outcomes, when handling file I/O.

    Paul
     
    Paul, Aug 18, 2008
    #9
  10. "Paul" <> wrote in message news:g8ajiu$uga$...

    [snip]

    > Defensive programming, means checking the values returned by things like
    > fscanf.
    > Wouldn't you be curious, whether fscanf converted zero, one, or two items
    > ? If the
    > answer is not two, then X or Y could contain bogus information. And if an
    > end of
    > file was encountered, you'd probably want to know about that also. There
    > are many
    > possible outcomes, when handling file I/O.
    >
    > Paul


    Like this you mean?

    fscanf(fp,"%.2f\t%.2f",&x,&y);
    if (fp==EOF)
    puts("fscanf error");


    Bill
     
    Bill Cunningham, Aug 18, 2008
    #10
  11. On Mon, 18 Aug 2008 00:49:03 GMT, "Bill Cunningham" <>
    wrote:

    >
    >"Bill Cunningham" <> wrote in message
    >news:yx3qk.131$w51.17@trnddc01...
    >
    >> Ok I did write this on the fly. I will look again at the actual code.
    >>

    >#include <stdio.h>
    >
    >int main() {
    > FILE *fp;
    > double x,y;
    > fp=fopen("zo","a");


    Are you reading from or writing to fp?

    > fscanf(fp,"%.2f\t%.2f",&x,&y);


    Do you have a reference that describes fscanf? Did you read it? Does
    fscanf allow "." inside a conversion specification? Is "f" the
    correct conversion specifier for a double?

    > fclose(fp);
    > printf("%.2f\t%.2f",x,y);
    >}
    >
    > Now this compiled for me with the results 0.00 and 4.87. Not the text


    What does the input file look like?

    >from the file called "zo". The only real difference here is the text mode is


    Difference from what?

    >append and not read.


    And you chose this mode why?

    --
    Remove del for email
     
    Barry Schwarz, Aug 18, 2008
    #11
  12. Bill Cunningham

    Ian Collins Guest

    Paul wrote:

    <sound advice>

    Don't spoon feed the troll :)

    --
    Ian Collins.
     
    Ian Collins, Aug 18, 2008
    #12
  13. Bill Cunningham

    Paul Guest

    Bill Cunningham wrote:
    > "Paul" <> wrote in message news:g8ajiu$uga$...
    >
    > [snip]
    >
    >> Defensive programming, means checking the values returned by things like
    >> fscanf.
    >> Wouldn't you be curious, whether fscanf converted zero, one, or two items
    >> ? If the
    >> answer is not two, then X or Y could contain bogus information. And if an
    >> end of
    >> file was encountered, you'd probably want to know about that also. There
    >> are many
    >> possible outcomes, when handling file I/O.
    >>
    >> Paul

    >
    > Like this you mean?
    >
    > fscanf(fp,"%.2f\t%.2f",&x,&y);
    > if (fp==EOF)
    > puts("fscanf error");
    >
    >
    > Bill
    >
    >


    fscanf also returns an integer value. You haven't
    taken advantage of the integer it returns.

    returned_value = fscanf(fp,"%.2f\t%.2f",&x,&y)

    The manual page says the returned_value can tell you some things
    about how things went, when the fscanf ran. You could check
    returned_value, to see how many arguments it got.

    This is not defensive programming, but you could
    do something like use "printf" to print the value
    of the integer "returned_value", and see whether it
    is the value you expected. If the value printed was 2,
    then you'd know you got two conversions, so both "x"
    and "y" got loaded with goodies.

    Try defining an integer called return_value, and
    see what is coming back from fscanf. Use printf
    to print out the value of "returned_value".

    Once you've figured out what went wrong, you can
    add conditional statements to your program, to
    protect it against invalid input or unexpected
    results like EOF.

    In your next posting, you can tell us what printf printed,
    and your interpretation of what it means.

    Paul
     
    Paul, Aug 18, 2008
    #13
  14. "Bill Cunningham" <> writes:
    > "Bill Cunningham" <> wrote in message
    > news:yx3qk.131$w51.17@trnddc01...
    >
    >> Ok I did write this on the fly. I will look again at the actual code.
    >>

    > #include <stdio.h>
    >
    > int main() {
    > FILE *fp;
    > double x,y;
    > fp=fopen("zo","a");
    > fscanf(fp,"%.2f\t%.2f",&x,&y);
    > fclose(fp);
    > printf("%.2f\t%.2f",x,y);
    > }
    >
    > Now this compiled for me with the results 0.00 and 4.87. Not the text
    > from the file called "zo". The only real difference here is the text mode is
    > append and not read.


    No, there at least two real differences. One is that you're not
    referring to an undeclared variable called "string". The other is
    that you change the mode for fopen from "r" (which would have been
    correct) to "a" (which makes no sense).

    "a" is append mode; it means you want to write new data to the end of
    an existing file. Why would you use append mode when you want to
    *read* from the file?

    BTW, do you expect us to *guess* what's in your "zo" file?

    --
    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, Aug 18, 2008
    #14
  15. "Bill Cunningham" <> writes:
    [...]
    > Like this you mean?
    >
    > fscanf(fp,"%.2f\t%.2f",&x,&y);
    > if (fp==EOF)
    > puts("fscanf error");


    No.

    What type is fp? What type is EOF? What makes you think that
    comparing fp to EOF is meaningful?

    Stop guessing. Get a decent reference and READ IT.

    If you have a copy of K&R2, my advice is to use it as your one and
    only reference. Don't waste your time with cvppreference.com; it's
    for C++.

    --
    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, Aug 18, 2008
    #15
  16. Bill Cunningham

    Guest

    On Aug 18, 5:23 am, Keith Thompson <> wrote:
    > "Bill Cunningham" <> writes:
    >
    > [...]
    >
    > > Like this you mean?

    >
    > > fscanf(fp,"%.2f\t%.2f",&x,&y);
    > > if (fp==EOF)
    > > puts("fscanf error");

    >
    > No.
    >
    > What type is fp? What type is EOF? What makes you think that
    > comparing fp to EOF is meaningful?
    >
    > Stop guessing. Get a decent reference and READ IT.
    >
    > If you have a copy of K&R2, my advice is to use it as your one and
    > only reference. Don't waste your time with cvppreference.com; it's
    > for C++.


    correction (typo): cppreference.com not cVppreference.com.
    Also, I'm not sure what cppreference.com is for, but it does not seem
    to be an appropriate resource for C++, so please don't suggest it.
    Just... don't waste time with cppreference :)
     
    , Aug 18, 2008
    #16
  17. writes:
    > On Aug 18, 5:23 am, Keith Thompson <> wrote:

    [...]
    >> Stop guessing. Get a decent reference and READ IT.
    >>
    >> If you have a copy of K&R2, my advice is to use it as your one and
    >> only reference. Don't waste your time with cvppreference.com; it's
    >> for C++.

    >
    > correction (typo): cppreference.com not cVppreference.com.


    Whoops, thanks for the correction.

    [...]

    --
    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, Aug 18, 2008
    #17
  18. On 18 Aug, 00:39, "Bill Cunningham" <> wrote:
    >     I'm doing something wrong and all I know to do is turn to clc. I have a
    > text file containing 2 doubles separated by a tab.
    >
    > .26    0
    >
    > Is the text. I want to read the two double and printf them out. Here's my
    > file.
    >
    > #include <stdio.h>
    >
    > int main() {
    >       FILE *fp;
    >       double x,y;
    >        fp=fopen("zo","r"); /*error checking out for brevity */
    >        fscanf(fp,"%.2f\t%.2f",&string);
    >        fclose(fp);
    >        printf("%.2f%.2f",x,y);
    >
    > }
    >
    > All I get is garbage that is contained in x and y. For whatever simple
    > reason that is beyond me evidently I can't read and printf out to stdin from
    > this text file. I don't think fread is really necessary.


    0. fix your layout
    1. post your code
    2. post your input data
    3. post your output
    4. explain why you don't like 3
    5. check return values
    6. RTFM
    7. don't guess (see 6)

    --
    Nick Keighley
     
    Nick Keighley, Aug 18, 2008
    #18
  19. "Keith Thompson" <> wrote in message
    news:...

    > BTW, do you expect us to *guess* what's in your "zo" file?
    >


    No Keith I've already said it contains,

    ..26 0.00

    In using the code I posted before I got...

    0.00 4.87

    If I would've set xand y to 0 that is what would've been printed with
    printf. I see the fopen mode I should've used was "r". I'll keep working
    with it.

    Bill
     
    Bill Cunningham, Aug 18, 2008
    #19
  20. "Paul" <> wrote in message news:g8akpm$ada$...

    [snip]

    >
    > fscanf also returns an integer value. You haven't
    > taken advantage of the integer it returns.
    >
    > returned_value = fscanf(fp,"%.2f\t%.2f",&x,&y)
    >
    > The manual page says the returned_value can tell you some things
    > about how things went, when the fscanf ran. You could check
    > returned_value, to see how many arguments it got.
    >
    > This is not defensive programming, but you could
    > do something like use "printf" to print the value
    > of the integer "returned_value", and see whether it
    > is the value you expected. If the value printed was 2,
    > then you'd know you got two conversions, so both "x"
    > and "y" got loaded with goodies.
    >
    > Try defining an integer called return_value, and
    > see what is coming back from fscanf. Use printf
    > to print out the value of "returned_value".
    >
    > Once you've figured out what went wrong, you can
    > add conditional statements to your program, to
    > protect it against invalid input or unexpected
    > results like EOF.
    >
    > In your next posting, you can tell us what printf printed,
    > and your interpretation of what it means.
    >
    > Paul


    Ok I'll try it. I'm not used to using the *scanf family. I usually use fgets
    or fgetc.

    Bill
     
    Bill Cunningham, Aug 18, 2008
    #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. cylin
    Replies:
    7
    Views:
    3,973
    Kevin Goodsell
    Sep 19, 2003
  2. Marc Reclaire

    Question about fscanf ..

    Marc Reclaire, Dec 18, 2003, in forum: C++
    Replies:
    2
    Views:
    397
    Karl Heinz Buchegger
    Dec 18, 2003
  3. John Phung

    fscanf equivalent in c++

    John Phung, Apr 6, 2004, in forum: C++
    Replies:
    11
    Views:
    19,365
    Nick Hounsome
    Apr 9, 2004
  4. Fallon

    fscanf question

    Fallon, Jul 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    394
    Giuseppe
    Jul 13, 2003
  5. Chris Torek
    Replies:
    0
    Views:
    405
    Chris Torek
    Jul 14, 2003
Loading...

Share This Page