我必须逐行将文件中的数据读入结构数组,但它会跳过第一行。我发现其他地方,因为fgets和sscanf组合第二行是第一次阅读,但我不明白为什么或如何我可以解决这个问题。任何建议或指向正确的方向都会很棒!
void readData(){
char line[50];
char name1[1][50];
int O, M, R, W;
input = fopen("input2.dat", "r");
//count = 0;
while(fgets(line, 50, input) != NULL){
hasReadData = 1;
sscanf_s(line, "%20s %d %d %d %d", name1[0], 50, &O, &M, &R, &W);
for(i=0; i<200; i++){
if(strcmp(name1[0], player_arr[i].name) == 0){
if(O<0 || M<0 || R<0 || W<0){
printf_s("%s %d %d %d %d", name1[0], O, M, R, W);
printf_s("\tNegative value, data invalid\n");
break;
}
if(O<M){
printf_s("%s %d %d %d %d", name1[0], O, M, R, W);
printf_s("\tOvers less than Maidens, data invalid\n");
break;
}
if(W>10){
printf_s("%s %d %d %d %d", name1[0], O, M, R, W);
printf_s("\tWickets greater than 10, data invalid\n");
break;
}
player_arr[i].oversOverall += O;
player_arr[i].maidensOverall += M;
player_arr[i].runsOverall += R;
player_arr[i].wicketsOverall += W;
player_arr[i].oversLatest = O;
player_arr[i].maidensLatest = M;
player_arr[i].runsLatest = R;
player_arr[i].wicketsLatest = W;
}
}
}
fclose(input);
}
答案 0 :(得分:-1)
如果你在读取文件的同时跳过第一行,那么在while循环中使用这个命令:
file_pointer.seekg(0,ios::beg);
此文件指针将指针指向文件的开头。