DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: fscanf for floats isn't working...

  1. #1
    Santos Guest

    fscanf for floats isn't working...


    What's wrong with this code???

    #include <stdio.h>
    #include <stdlib.h>

    FILE *fdEntrada;
    int Total_Vertices;
    int intCtrl;
    int X;
    int Y;
    int Z;
    int i;

    int main(int argc, char** argv)
    {
    if ((fdEntrada = fopen ("Points.txt", "r")) == NULL)
    {
    printf ("Error, invalid File Name\n");
    exit (0);
    }

    fseek (fdEntrada, 0L, SEEK_SET);

    fscanf (fdEntrada, "%d\n", &Total_Vertices);
    printf ("\nTotal Vertices = %d\n",Total_Vertices);

    for (i = 0; i < Total_Vertices; i++)
    {
    fscanf (fdEntrada, "%f\n%f\n%f\n", &X, &Y, &Z);
    printf ("X = %f Y = %f Z = %f \n",X,Y,Z);
    }
    while (1 == 1){}
    return 1;
    }

    And Points.txt is:

    5
    1
    -200.000000
    0.000000
    0.000000
    1
    -196.792535
    0.000000
    -22.437673
    1
    -187.636682
    0.000000
    -42.382271
    1
    -173.232250
    0.000000
    -59.833795
    1
    -154.279049
    0.000000
    -74.792244



  2. #2
    Luis Abreu Guest

    Re: fscanf for floats isn't working...

    Hi.

    Try doing this:

    #include <stdio.h>
    #include <stdlib.h>

    FILE *fdEntrada;
    int Total_Vertices;
    int intCtrl;
    float X;
    float Y;
    float Z;
    int i;

    int main(int argc, char** argv)
    {
    if ((fdEntrada = fopen ("Points.txt", "r")) == NULL)
    {
    printf ("Error, invalid File Name\n");
    exit (0);
    }

    fseek (fdEntrada, 0L, SEEK_SET);

    fscanf (fdEntrada, "%d", &Total_Vertices);
    printf ("\nTotal Vertices = %d\n",Total_Vertices);

    for (i = 0; i < Total_Vertices; i++)
    {

    fscanf( fdEntrada, "%f", &X );
    fscanf( fdEntrada, "%f", &Y );
    fscanf( fdEntrada, "%f", &Z );
    printf ("X = %f Y = %f Z = %f \n",X,Y,Z);
    }

    return 1;
    }

    If you want to read floats, then you must have float variables. scanf
    is one of the functions that I really don't like much. For instance,
    don't put code like this on it;
    scanf( "%d\n", &a);
    \n is doing nothing there!
    Also you must be aware that scanf, by default, reads until it finds a
    blank space or till it finds the end of line.
    It reads the buffer (which only has chars on it) and converts it to
    appropriate values of the type specified.
    Just one more thing: it's always better (in my opinion) to use only
    one scanf per variable. If you do this, you can always use fflush() to
    flush the buffer if you have problems getting input.
    fscanf has similar behaviour, but instead of reading from standard
    input buffer (stdin), reads from a file.
    Just one more thing: what was the idea of putting an infinite cyle at
    the end of the program?

    Hope this helps

    Luis Abreu

    On 28 Nov 2000 19:34:29 -0800, "Santos" <eaas@rnl.ist.utl.pt> wrote:

    >
    >What's wrong with this code???
    >
    >#include <stdio.h>
    >#include <stdlib.h>
    >
    >FILE *fdEntrada;
    >int Total_Vertices;
    >int intCtrl;
    >int X;
    >int Y;
    >int Z;
    >int i;
    >
    >int main(int argc, char** argv)
    >{
    > if ((fdEntrada = fopen ("Points.txt", "r")) == NULL)
    > {
    > printf ("Error, invalid File Name\n");
    > exit (0);
    > }
    >
    > fseek (fdEntrada, 0L, SEEK_SET);
    >
    > fscanf (fdEntrada, "%d\n", &Total_Vertices);
    > printf ("\nTotal Vertices = %d\n",Total_Vertices);
    >
    > for (i = 0; i < Total_Vertices; i++)
    > {
    > fscanf (fdEntrada, "%f\n%f\n%f\n", &X, &Y, &Z);
    > printf ("X = %f Y = %f Z = %f \n",X,Y,Z);
    > }
    > while (1 == 1){}
    > return 1;
    >}
    >
    >And Points.txt is:
    >
    >5
    >1
    >-200.000000
    >0.000000
    >0.000000
    >1
    >-196.792535
    >0.000000
    >-22.437673
    >1
    >-187.636682
    >0.000000
    >-42.382271
    >1
    >-173.232250
    >0.000000
    >-59.833795
    >1
    >-154.279049
    >0.000000
    >-74.792244
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center