Thread: Help with strtod and fgets..

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    64

    Help with strtod and fgets..

    Hey
    I need help with strtod, can anybody help me..?

    I need to read a lot of coordinates from a file, whicj will be
    translatet into double with strtod, and make a circle move
    around the screen, with the coordinates from the file.
    But i'm a bit unsure, how to use strtod and read the date from the file.
    I have x and y, which shall make the circle go.
    Can anybody give me some directions about fgets and how to
    get the coordinates into the right x and y coordinates..

    tx
    Gugge
    ------------code ---------------------
    x = 150.0;
    y = 200.0;

    drawCircle(x, y, BLACK);
    drivecoord = fopen("xycoord.txt","r");

    if(!drivecoord)
    {
    printf("FEJL - Filen kunne ikke findes.\n");
    return 0;
    }

    while(!feof(drivecoord))
    {
    quitCheck ();

    fgets(coord,80,drivecoord);
    value = strtod(coord, &endptr);

    printf("The string is %s the number is %lf\n", coord, value);

    drawCircle(x, y, BLACK);

    }
    ---------------------------
    !G!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >while(!feof(drivecoord))
    don't do this! It'll give you trouble. Do a board search on how to check the return code of fgets() to properly determine when to stop reading a file.

    Haven't look at your question though <sorry>!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you have pairs of coordinates on the same line, then try

    Code:
    while ( fgets(coord,sizeof(coord),drivecoord) != NULL ) {
      x = strtod(coord, &endptr);
      y = strtod(endptr,NULL);
      drawCircle(x, y, BLACK); 
    }

Popular pages Recent additions subscribe to a feed