Thread: Read from file a float

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52

    Read from file a float

    The contents of the file are:
    Code:
    dim 400
    epsilon 0.001
    ....
    the code is:
    Code:
        // read the dimension of the table
        fscanf(fp, "dim %d", &dimension);
        fprintf(stderr, "the dimension is %d\n", dimension);
        //printf("the dimension is %d\n", dimension);
        // read the fault tolerance (epsilon)
        fscanf(fp, "epsilon %f", &eps);
        fprintf(stderr, "epsilon is %f\n", eps);
    It doesn't seem to be working for the float number but works for the integer. What's wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    fscanf returns a result, maybe check what that is?

    TBH, use fgets() and sscanf for a more predictable outcome. If fscanf() errors out half-way through a line, you've got no idea where you are.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    Thanks Salem. Breaking it up into pieces helped me to find the error.
    The fscanf() was reading the new line character and not the next line.
    The code which does the work is:
    Code:
        fscanf(fp, "dim %d", &dimension);
        fprintf(stderr, "the dimension %d\n", dimension);
        getc(fp);
        fscanf(fp, "epsilon %lf", &eps);
        fprintf(stderr, "epsilon is %f\n", eps);

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, it's better to just use fgets. Then you don't have to worry about crap stuck in the input buffer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Greece
    Posts
    52
    I'll keep it in mind Elysia.
    Isn't there any speed trade-off using these two statements instead of one?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You hardly need to worry about the performance.
    If performance is low, then you would use a profiler to find the real bottleneck, which may or maybe not be what you think it is.
    Don't worry about optimization prematurely.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM