> fscanf(fp, "%4[^:]:%12[^:]:%12[^:]:%d:%lf\n", employeeNum[i], firstName[i], lastName[i], &payGroup, workedHours[i]
The fscanf function requires pointers to the variables you want it to change. Inteerestingly, fscanf is also a good choice for reading this particular format.
Also, fp is a pointer, which can only be compared with other file pointers. fp will never be EOF.
I'm not really sure you need 2d array for half of these variables. Let's see if we can simplify a little bit:
Code:
#include <stdio.h>
#include <stdlib.h>
struct employee_t
{
char employeeNumber[5];
char lastName[13];
char firstName[13];
int payGroup;
float hoursWorked;
};
int
process( struct employee_t ar[6], FILE * finp )
{
if( finp != NULL ) {
size_t idx = 0;
int scan = 0;
do {
scan = fscanf( finp, "%4[^:]:%12[^:]:%12[^:]:%d:%f\n",
&ar[idx].employeeNumber, &ar[idx].lastName, &ar[idx].firstName,
&ar[idx].payGroup, &ar[idx].hoursWorked );
if( ferror( finp ) ) {
printf( "Error reading input file line %ul\n", ( unsigned long )idx + 1 );
return -1;
}
idx++;
}
while( scan == 5 );
return 0;
}
return 1;
}
int
main( void )
{
struct employee_t my_employees[6];
const char * filepath = "C:/Documents and Settings/JCK/Desktop/samp.txt";
FILE * finp = fopen( filepath, "r" );
size_t idx = 0;
if( finp != NULL ) {
if( process( my_employees, finp ) == 0 ) {
for( idx = 0; idx < sizeof my_employees / sizeof my_employees[0]; idx++ )
printf( "%s: %s, %s\tGroup: %2d Hours: %2.2f\n",
my_employees[idx].employeeNumber,
my_employees[idx].lastName,
my_employees[idx].firstName,
my_employees[idx].payGroup,
my_employees[idx].hoursWorked );
fclose( finp );
}
}
else {
printf( "Couldn't open input file\n" );
}
return 0;
}
There. Correct, errors all nice and handled, and the file format's been read. Don't always jump to 2d or 3d arrays: often you dont need them.