-
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
-
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.
-
> 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]);