尝试从文件扫描到 C 数组,但出现错误。当我使用每行只有数字的文件时,它对我有用,例如:
1.2
3.4
5.2
但现在我有这个文件:
0001:Satriani:Joe:6:38.0
0002:Vai:Steve:1:44.5
0003:Morse:Steve:10:50.0
0004:Van Halen:Eddie:3:25.75
0005:Petrucci:John:8:42.25
0006:Beck:Jeff:3:62.0
这是我尝试将其扫描到数组中的方法,但出现分段错误!
FILE *employeesTXT;
int empID[100];
char lastName[100];
char firstName[100];
int payGroup[100];
double hoursWorked[100];
employeesTXT = fopen("employees.txt", "r");
if (employeesTXT == NULL)
{
printf("Error: file cannot be opened.\n");
} else {
while (fscanf(employeesTXT, "%[^:]:%[^:]:%[^:]:%d:%lf\n", &empID[i], lastName[i], firstName[i], &payGroup[i], &hoursWorked[i]) != EOF)
{
i++;
}
fclose(employeesTXT);
numRecords = i;
for(i = 0; i < numRecords; i++){
printf("%d - %s - %s - %d - %.2lf\n", empID[i], lastName[i], firstName[i], payGroup[i], hoursWorked[i]);
}
}
它必须是这一行的东西...... %[^:]:%[^:]:%[^:]:%d:%lf\n