【问题标题】:fscanf wont work - Program is casting an errorfscanf 无法工作 - 程序正在投射错误
【发布时间】:2016-10-19 15:32:07
【问题描述】:

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

【问题讨论】:

  • 这是什么烂摊子?您的 scanf 格式字符串仅用于两个参数。而你正在像无数人一样过去。
  • 该结构的所有其他成员的值将是不确定的,因为结构temp 未初始化。 UB。
  • 我不明白你只用两个格式标签读入10变量是什么意思:while(fscanf(dataBase, "%s %f", &amp;temp.patientNumber, &amp;temp.patientGender, &amp;temp.patientLength, &amp;temp.patientWeigth, &amp;temp.patientHeadCircuit, &amp;temp.patientApperance, &amp;temp.patientPulse, &amp;temp.patientGrimace, &amp;temp.patientActivity, &amp;temp.patientRespiration)?
  • %f for patientGender?嗯......这是一个假设;)
  • 使用 %s 读取患者编号(声明为 char[5])是危险的。如果输入的字符超过 4 个怎么办? (答案:你会在分配的空间之外写)。

标签: c scanf


【解决方案1】:

我了解到您指的是链接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];

【讨论】:

  • 好吧,我不介意在 while 循环中的 patientNumber 之前删除 '&',并且 patientTab 需要 malloc,但现在一切正常。谢谢你:)
  • 它也适用于 realloc.. 只需将第一行 PATIENT *patientTab; 更改为 PATIENT *patientTab = NULL;
  • realloc 仍然存在,我只是输入了类似“patientTab = malloc(5 * sizeof(PATIENT));”的行在 while 循环之前,因为带有 realloc 的片段导致错误......但是,我认为放置“null”也是一件好事:D
  • 答案可能毫无疑问是有效的,但如果它没有解释“为什么”(即使只是一个指向 fscanf 手册页的链接),它可能就没有那么有用了。 6 而不是 5 也是如此 - OP 将只是通过死记硬背来学习一个咒语,或者通过反复试验来学习编程(“哦,等等,让我们让缓冲区更长一点”)。
猜你喜欢
  • 2020-03-26
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
  • 2017-03-17
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
相关资源
最近更新 更多