[英]I am having trouble with reading input from a file into a structure of arrays
這是我遇到的問題。 基本上,結構定義正確,但是編譯器在嘗試從給定文件中讀取輸入時拋出錯誤。 我可以在案件之前的while循環中瀏覽菜單,然后執行選項“ a”或“ A”並獲取患者編號。 然后,我可以打開文件,但是無法從文件中將任何信息讀取到結構變量中。 它只是終止代碼。 我嘗試從中讀取的文件格式如下:time BP_diastolic BP_systolic 01 80 120 02 81 124 03 78 118等。
時間temp 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 <𝐵𝑃𝐷≤89𝑜𝑟120 <𝐵𝑃𝑆≤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;
}
您的代碼中有很多事情不會被編譯器接受-您可以進行一些改進:
"
代替'
fp
和fileName
,並且BP
被聲明為類型別名(使用typedef
)而不是變量。 else
說法,而不是結束與梅開二度該條件的如果部分}
。 fopen()
不接受為您提供的文件名的構造-您必須首先在對sprintf()
的單獨函數調用中執行此操作。 if (fp = fopen(...) == NULL)
的構造並不太激動,盡管也許其他編譯器可能接受它。 該語句可以拆分。 fscanf()
語句中,有三件事:
[i]
放在錯誤的位置, i
沒有定義,也沒有增加到下一個值,也沒有檢查它是否達到最大值 %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它不喜歡所有的東西ARN)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.