我无法将文件中的输入读取到数组结构中

时间:2018-12-01 16:27:48

标签: c codeblocks

这是我遇到的问题。基本上,结构定义正确,但是编译器在尝试从给定文件中读取输入时抛出错误。我可以在案件之前的while循环中浏览菜单,然后执行选项“ a”或“ A”并获取患者编号。然后,我可以打开文件,但是无法从文件中将任何信息读取到结构变量中。它只是终止代码。我尝试从中读取的文件格式如下: 时间BP_舒张压BP_收缩压 01 80 120 02 81124 03 78 118 等等

时间温度 01 98.7 02 99.0 03 98.5 等

时间率 01 68 02 70 03 65 等等

我应该从文件中读取信息,将其放入变量中,然后根据以下公式确定给定日期的健康评分: (,,)= 15 ∗ + 35 ∗ + 25 ∗ +25

其中: temp基于时间t的体温,并定义为:= {1 97≤≤99 0ℎ
 压力基于时间t的血压,定义为:  = { 1≤80≤120 0.5 80 <≤89120 <≤139 0≥90≥140
 速率基于在t时刻结束的心率,基于某个∆t,通常为15至60秒,并定义为:= {1 60≤≤100 0

我还没有到达那部分,因为我不知道如何将信息放入变量中。因此,我无法使用所述数据进行任何计算。

如果选择了选项d),则程序应要求用户提供一个时间点来计算健康分数。输入有效时间后(即应在给定的时间间隔内),程序应显示: 一种。包含BT,BP和HR值的表格, b。检测任何状况的报告(即正常/高血压前期/    高血压),以及 C。使用公式获得的HealthScore值。 1.

如果选择选项e),则应显示一个表格,其中包含每个变量的统计参数(在时间间隔内)。这包括最小,最大,平均和标准偏差。

还应该进行测试,以确保程序的所有条目都在适当的范围内。

我希望这是对我所遇到问题的更好解释。另外,这时的作业迟到了,但我仍然想了解我做错了什么。任何帮助将非常感激。

    #include<stdio.h>
    #include<stdlib.h>


    struct bloodPressure{
       int time;
       double pressD;
       double pressS;
    };

    struct temp{
       int time;
       double temp;
    };

    struct rate{
       int time;
       double r;
    };

    struct bloodPressure BP[55];
    struct temp T[55];
    struct rate R[55];
    int main()
      {
            int timeStart,
                timeEnd,
                timeSelect;
          double minTemp,
                 maxTemp,
                 avgTemp,
                 stdTemp,
                 minPressD,
                 maxPressD,
                 avgPressD,
                 stdPressD,
                 minpressS,
                 maxPressS,
                 avgPressS,
                 stdPressS,
                 minRate,
                 maxRate,
                 avgRate,
                 stdRate,
                 score;
          char fileName[140],
               fileNameBuffer[150],
               patientNum[5],
               menuSelect = "a";
          FILE *fp;



          while (menuSelect)
          {
             printf("Please select from the following list:\n");
             printf("a/A: Enter patient number\n");
             printf("b/B: Enter file folder location\n");
             printf("c/C: Enter time interval\n");
             printf("d/D: Get health score\n");
             printf("e/E: Get statistical data\n");
             printf("f/F: Exit the program\n");
             scanf("%c", &menuSelect);

             switch(menuSelect)
             {
                // code to enter patient ID number
                case 'a':
                case 'A':
                {
                   // ask user for the patient's ID number
                   printf("Please enter the Patient's ID number:\n");
                   scanf("%s", &patientNum);

                   printf("%s", patientNum);

                   // break out of switch statement and return to main                 
                   // menu
                   break;
                }

                // code to enter file location
                case 'b':
                case 'B':
                {
                   printf("Please enter the file location:\n");
                   scanf("%s", &fileName);

                   // if file for blood pressure does not open, display 
                   // error and return to main menu
                   sprintf(fileNameBuffer, "%sBP_%s.txt", fileName, 
                   patientNum);
                   fp = fopen(fileNameBuffer, "r");
                   printf("%s \n", fileNameBuffer);
                   if (fp == NULL)
                   {
                      printf("cannot open file");
                   }

                   // if file opens, read information from the file
                   // else
                   {
                      printf("file is being read\n");
                      // read in variables from blood pressure file into 
                      // blood pressure structure
                      while (!feof (fp))//int i = 0; i < 55; i++)
                      {
                         int i = 0;
                         fscanf(fp, '%d %lf %lf', BP[i].time, 
                         BP[i].pressD, BP[i].pressS);
                         printf("%d", BP[i].time);
                         if (++i >= 55)
                         {
                            break;
                         }
                      }

                      // close the blood pressure file
                      fclose(fp);
                   }

                   // if file for blood temperature does not open, display 
                   // error and return to main menu
                   if (fp = fopen(("%sBT_%d.txt", fileName, patientNum), 
                   'r') == NULL)
                   {
                      printf("cannot open file");
                   }

                   // if file opens, read information from the file
                   else
                   {
                      // read in variables from heart rate file into blood 
                      // pressure structure
                      while (!feof (fp))
                      {
                         int i = 0;
                         fscanf(fp, '%d %lf', &(T[i].time), &(T[i].temp));
                         if (++i >= 55)
                         {
                            break;
                         }
                      }

                      // close the heart rate file
                      fclose(fp);
                   }

                   // if file for heart rate does not open, display error 
                   // and return to main menu
                   if (fp = fopen(("%sHR_%d.txt", fileName, patientNum), 
                   'r') == NULL)
                   {
                      printf("cannot open file");
                   }

                   // if file opens, read information from the file
                   else
                   {
                      // read in variables from heart rate file into blood         
                      // pressure structure
                      while (!feof (fp))
                      {
                         int i = 0;
                         fscanf(fp, '%d %f', &(R[i].time), &(R[i].r));
                         if (++i >= 55)
                         {
                            break;
                         }
                      }

                      // close the blood pressure file
                      fclose(fp);
                   }

                   // break out of switch statement and return to main 
                   // menu
                   break;
                }

                // code to enter time interval
                case 'c':
                case 'C':
                {
                   // ask user for start and end times for the time 
                   // interval
                   printf("Please enter the starting time:\n");
                   scanf('%d', timeStart);
                   printf("Please enter the ending time:\n");
                   scanf('%d', timeEnd);

                   // break out of switch statement and return to main 
                   // menu
                   break;
                }

                // code to calculate health score
                case 'd':
                case 'D':
                {

                   break;
                }

                // code to calculate statistical data
                case 'e':
                case 'E':
                {

                   break;
                }

                // code to exit the program
                case 'f':
                case 'F':
                {
                   printf("You have chosen to exit the program\n");
                   exit(1);
                }

                default:
                {
                   printf("Error: Please enter a valid menu option (a-f or 
                   A-F)");
                   break;
                }
             }
          }



        return 0;
      }

1 个答案:

答案 0 :(得分:0)

问题更改后进行了编辑-见下文


编译器无法接受代码中的许多内容-您可以进行以下改进:

  1. 字符串在C中需要双引号,因此"而不是'
  2. 变量应在使用前声明。例如,在您的代码中,未声明fpfileName,并且BP被声明为类型别名(使用typedef)而不是变量。
  3. 有一些小错误,例如注释else语句,而不用大括号}结束该条件的if部分。
  4. fopen()不接受为您提供的文件名的构造-您必须首先对sprintf()进行单独的函数调用。
  5. 我的编译器对结构if (fp = fopen(...) == NULL)并不太激动,尽管也许其他编译器也可以接受。该语句可以拆分。
  6. fscanf()语句中,有三件事:
    • 索引[i]放在错误的位置,
    • i未定义,也没有增加到下一个值,也没有检查它是否达到最大值
    • 小数点:要读取 double 类型的值,请使用扫描字符串%lf(长浮点数)。

开始时C是一种非常难懂的语言,所以不用担心...这是我用它制作的(在我的计算机上可以正常工作):

#include <stdio.h>

// structure definitions
struct bloodPressure{
    int time[55];
    double pressD[55];
    double pressS[55];
};

// declarations
struct bloodPressure BP;
char fileName[100], fullname[110];
int i = 0;
const int patientNum = 1;
FILE *fp;

// start of problem
int main() {
    printf("Please enter the file location:\n");
    scanf("%s", fileName);
    sprintf(fullname, "%sBP_%d.txt", fileName, patientNum);

    // if file for blood pressure does not open, display error and 
    // return to main menu
    fp = fopen(fullname, "r");
    if (fp == NULL)
    {
        printf("cannot open file");
    }

    // if file opens, read information from the file
    else
    {
        // read in variables from blood pressure file into blood 
        // pressure structure
        while (!feof (fp))    //int i = 0; i < 55; i++
        {
            fscanf(fp, "%d %lf %lf", &(BP.time[i]), &(BP.pressD[i]), &(BP.pressS[i]));
            if (++i >= 55) break;
        }
        // close the blood pressure file
        fclose(fp);
    }
}

您可能会考虑更改struct bloodPressure的定义,以便它保存一个测量的压力数据,然后创建此类结构的数组,例如

struct bloodPressure BP[55];

一个拥有数组的结构代替了这里的结构。对我来说似乎更合逻辑。


编辑

当您重写了问题和程序(并认为我做了很多改进)时,我正在编辑答案。

  

我可以打开文件,但不能从文件中将任何信息读取到结构变量中。它只是终止代码。

在109行中,您的fscan扫描模式仍然是单引号(引号应为")。在其他一些地方也一样,例如188行。

顺便说一句,您在第101行中的else语句仍被注释掉。

通常,您的编码正在快速改进。如果告诉编译器在执行可能导致错误的操作时警告您,则可能会有所帮助。许多编译器接受选项-Wall W 了解所有它不喜欢的东西)。