fscanf

This is a discussion on fscanf within the C++ Programming forums, part of the General Programming Boards category; HI! I am trying to do a homework assignment that is causing much frustration. The assignment is to sort through ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    Unhappy fscanf

    HI!
    I am trying to do a homework assignment that is causing much frustration. The assignment is to sort through 4 arrays (emp_id[MAX], wage[MAX], hours[MAX], week_pay[MAX]. Calculate the week_pay and sort in descending order by emp_id. The problem I now have is that the program is not scanning the information. Here is the code I am using....

    infile = fopen("a:\\empout.txt", "r");
    if (infile = NULL)
    {
    print error msg
    exit(1);
    }
    input_status = fscanf(infile, "%ld %f %f",&emp_id[index],&wage[index],&hours[index]);
    while (input_status != MAX)
    {
    index = index + 1;
    input_status = fscanf(infile, "%ld %f %f",&emp_id[index],&wage[index],&hours[index]);
    }
    *record_num = index;
    fclose(infile);
    }

    I declared this functin as: void read_record(long *emp_id, float *wage, float*hours, int *record_num);

    When I call the function: read_record(emp_id, wage, hours, &record_num);

    Is there anything wrong with this code. When I run a watch these variables show the address no problem but nothing changes the info is not being scanned. This program basically crashes everytime. I can't find anything wrong with the code but maybe I'm missing something. Any help would be appreciated.
    Thanks

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    fscanf returns the number of arguments successfully assigned to variables so in your example input_status should end up with a value of 3 each time you use it so if MAX is not 3 your while loop will run forever if it is 3 it won't loop at all. It only returns EOF if an error has occured.

    try this:
    Code:
    index = 0;
    while(index < MAX)
    {
    fscanf(infile,"%ld %f %f", emp_num[index],wage[index],hours[index]);
    index++;
    }
    You also used the & in front of your array's in the fscanf, no need for them.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > You also used the & in front of your array's in the fscanf, no need for them.
    Oh, but you do - it's not an array, its an array element, and so you do need an &

    fscanf(infile,"%ld %f %f",
    &emp_num[index], &wage[index], &hours[index]);
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-28-2008, 11:11 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. fscanf help
    By Axel in forum C Programming
    Replies: 13
    Last Post: 10-22-2006, 10:44 PM
  4. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 01:32 PM
  5. fscanf on sun's
    By brif in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 01:22 PM

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