我在 fscanf
函数中遇到一些问题。我是 C 语言新手,我想从文件中读取行并将它们保存到结构中,但看来我不太理解它 ^^。
PATIENT *patientTab;
int tabSize = 0;
int patientCount = 0;
PATIENT temp;
int tempIndex = 0;
if (openFile()){
printf("Plik otworzony!\n\n");
while(fscanf(dataBase, "%s %f",
&temp.patientNumber,
&temp.patientGender,
&temp.patientLength,
&temp.patientWeigth,
&temp.patientHeadCircuit,
&temp.patientApperance,
&temp.patientPulse,
&temp.patientGrimace,
&temp.patientActivity,
&temp.patientRespiration)
!= EOF){
if (patientCount + 1 >= tabSize){
tabSize += 5;
patientTab = realloc(patientTab, sizeof(int) * tabSize);
}
for (tempIndex; tempIndex < 5; tempIndex++){
patientTab[patientCount].patientNumber[tempIndex] = temp.patientNumber[tempIndex];
}
patientTab[patientCount].patientGender = temp.patientGender;
patientTab[patientCount].patientLength = temp.patientLength;
patientTab[patientCount].patientWeigth = temp.patientLength;
patientTab[patientCount].patientHeadCircuit = temp.patientHeadCircuit;
patientTab[patientCount].patientApperance = temp.patientApperance;
patientTab[patientCount].patientPulse = temp.patientPulse;
patientTab[patientCount].patientGrimace = temp.patientGrimace;
patientTab[patientCount].patientActivity = temp.patientActivity;
patientTab[patientCount].patientRespiration = temp.patientRespiration;
patientCount++;
}
//free(temp);
fclose(dataBase);
}
else endProgram();
数据库是全局性的。
问题是当程序尝试循环“while”(其中包括“fscanf”函数)时,程序会抛出错误,我唯一能做的就是关闭程序。我在互联网上找到了一个例子,但这是一个只有一个变量的简单例子。我确信问题出在我的声明中包含许多带有“&temp”的参数。
结构是:
typedef struct Patient {
char patientNumber[5];
char patientGender;
double patientLength;
float patientWeigth;
float patientHeadCircuit;
int patientApperance;
int patientPulse;
int patientGrimace;
int patientActivity;
int patientRespiration;
}PATIENT;
...这是内容文件的片段: txt link
最佳答案
据我了解,您指的是链接 http://www.beetxt.com/BMB/ 中的内容
请更改 1)
while(fscanf(dataBase, "%s %f",
&temp.patientNumber,
&temp.patientGender,
&temp.patientLength,
&temp.patientWeigth,
&temp.patientHeadCircuit,
&temp.patientApperance,
&temp.patientPulse,
&temp.patientGrimace,
&temp.patientActivity,
&temp.patientRespiration)
与
while(fscanf(dataBase, "%s %c %lf %f %f %d %d %d %d %d",
temp.patientNumber,
&temp.patientGender,
&temp.patientLength,
&temp.patientWeigth,
&temp.patientHeadCircuit,
&temp.patientApperance,
&temp.patientPulse,
&temp.patientGrimace,
&temp.patientActivity,
&temp.patientRespiration)
2)
if (patientCount + 1 >= tabSize){
tabSize += 5;
patientTab = realloc(patientTab, sizeof(int) * tabSize);
}
与
if (patientCount + 1 >= tabSize){
tabSize += 5;
patientTab = realloc(patientTab, sizeof(PATIENT) * tabSize);
}
3)
for (tempIndex; tempIndex < 5; tempIndex++){
patientTab[patientCount].patientNumber[tempIndex] = temp.patientNumber[tempIndex];
}
patientTab[patientCount].patientGender = temp.patientGender;
patientTab[patientCount].patientLength = temp.patientLength;
patientTab[patientCount].patientWeigth = temp.patientLength;
patientTab[patientCount].patientHeadCircuit = temp.patientHeadCircuit;
patientTab[patientCount].patientApperance = temp.patientApperance;
patientTab[patientCount].patientPulse = temp.patientPulse;
patientTab[patientCount].patientGrimace = temp.patientGrimace;
patientTab[patientCount].patientActivity = temp.patientActivity;
patientTab[patientCount].patientRespiration = temp.patientRespiration;
patientCount++;
与
for (tempIndex; tempIndex < 5; tempIndex++){
strcpy(patientTab[patientCount].patientNumber[tempIndex] = temp.patientNumber[tempIndex]);
patientTab[patientCount].patientGender = temp.patientGender;
patientTab[patientCount].patientLength = temp.patientLength;
patientTab[patientCount].patientWeigth = temp.patientLength;
patientTab[patientCount].patientHeadCircuit = temp.patientHeadCircuit;
patientTab[patientCount].patientApperance = temp.patientApperance;
patientTab[patientCount].patientPulse = temp.patientPulse;
patientTab[patientCount].patientGrimace = temp.patientGrimace;
patientTab[patientCount].patientActivity = temp.patientActivity;
patientTab[patientCount].patientRespiration = temp.patientRespiration;
patientCount++;
}
希望这一切正常..并将 char PatientNumber[5];
更改为 char PatientNumber[6];
关于c - fscanf 无法工作 - 程序正在抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37887592/