Thread: fscanf

  1. #1
    Registered User
    Join Date
    Oct 2001

    Unhappy fscanf

    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
    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;

    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.

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    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:
    index = 0;
    while(index < MAX)
    fscanf(infile,"%ld %f %f", emp_num[index],wage[index],hours[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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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.

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
Website Security Test