sscanf func. used on float ??

This is a discussion on sscanf func. used on float ?? within the C Programming forums, part of the General Programming Boards category; Can't it be done, to use sscanf on af textfile where some float numbers are. I can't make it work?? ...

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

    sscanf func. used on float ??

    Can't it be done, to use sscanf on af textfile where some float numbers are.
    I can't make it work??

    Gugge

    my test code....

    #define TEXTFILE "data.txt"

    int main()
    {
    FILE *infile;
    char talbuffer[1000];

    struct data
    {
    char name[40];
    double a,b;
    int id;
    }rec[80];

    printf("Her starter det\n\n");

    if ((infile = fopen(TEXTFILE, "r")) !=NULL)
    {
    while(fgets(talbuffer,(int)sizeof talbuffer, infile) !=NULL)
    {
    if(sscanf(talbuffer, "%d,%[^,], %f", &rec->id, &rec->name, &rec->a) == 3)

    /* printf("%s\n", talbuffer);*/
    printf("%d, %s, %f\n", rec->id, rec->name, rec->a);
    }
    }

    getch();
    return 0;
    }

    my datafile:

    1, krummer, 12.34
    2, toast, 234.456
    3, citroner, 345.343
    4, sodavand, 5.32
    5, is, 567.99
    6, fodbold, 0.3435
    7, mennesker,121.21
    8, duer, 56.34
    9, hunde, 0.7457320
    10, kantsten, 12.12
    !G!

  2. #2
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Use %lf in format string, instead of %f

    If you want to assign each line in textfile to a structure in array rec you will have to find some means of increasing rec pointer. Something like rec++ in each iteration. Or even better, define a new pinter and initilize it as ptr = rec and then increase ptr inside loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21