Thread: Problem with fscanf and %lf

  1. #1
    Registered User
    Join Date
    Dec 2012

    Problem with fscanf and %lf

    I am doing a school assignment where we are to open a file, read in some information to a struct type, and do some calculations with it. The only problem I am having, is it seems that I am unable to fscanf the value 10.00 into a double type. The first code is my work, the printf's in the for loop are only there to emphasize the problem. The second code is the file I am reading from. I am absolutely clueless to the problem. Any help would be GREATLY appreciated, as this is due today.

    int main(void)
        FILE *time;
        char fileName[BUFFER];
        int num_employees, x, i;
        printf("Enter filename: ");
        scanf("%s", &fileName);
        time = fopen(fileName, "r");
        fscanf(time, "%d", &num_employees);
        struct employee workers[num_employees-1];
        for(i = 0; i < num_employees; i++)
                  fscanf(time, "%s", &workers[i].first);
                  printf("%s", workers[i].first);
                  fscanf(time, "%s", &workers[i].last);
                  printf("%s", workers[i].last);
                  fscanf(time, "%lf", &workers[i].payperhr);
                  printf("%4.2f", workers[i].payperhr);
        return 0;
    John Slacker 5.15
    Jane Worker 10.00

  2. #2
    Registered User
    Join Date
    Sep 2012
    Why do you remove 1 from num_employees in line 12?
    Without that, the for loop executes one less time than required (and the array size is too short).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  2. fscanf problem
    By yasharm in forum C Programming
    Replies: 8
    Last Post: 05-02-2007, 05:20 PM
  3. fscanf problem
    By srtonline in forum C Programming
    Replies: 4
    Last Post: 11-21-2006, 10:39 AM
  4. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 06:24 AM
  5. Problem with fscanf..
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 01-11-2002, 02:56 PM
Website Security Test