c - fscanf 无法工作 - 程序正在抛出错误

标签 c scanf

我在 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/

相关文章:

C - 在多个函数中使用结构

c - FILE 是常量或结构或两者

c - printf 不在控制台上打印

c++ - 使用 fscanf 或等价物扫描字符串

c++ - 应用于字符串的 std::cin 和 scanf() 之间的区别

c - scanf() 跳过变量

c - 为什么我的打印函数会用以下代码中的最后一个条目覆盖以前的条目?

c - 当我在 Xcode 中使用命令行工具输入内容时,代码会在我第一次输入时忽略它。为什么会这样,有没有办法避免呢?

c - 如何将字符更改为整数?

我可以在 C 中同时扫描和打印整数吗?