输出文件从输入拉出零

时间:2017-01-24 18:20:43

标签: c++

好吧,我现在已经潜伏了一段时间,试图读起来并弄清楚我的问题,但我很难过。

我正在尝试从bloodData中提取数据,这是一个具有一堆整数格式的dat文件:

4122 90 78 50

3123 110 92 83 95 77

3183 123 94 97 99

然而,当我运行我的提取时,我得到的只是0.我假设我正在读错我的文件。我思考的缺陷在哪里?我只用了两周时间学习C ++。

    while(bloodData.good())                      //Need to write while !EOF (changed: while True)
    {
        bloodData >> patientID;                 //Get patient ID (stop at first whitespace)
        patientsBloodData << patientID;         //Output patients ID
        while(getchar() != '\n')                //Need to write while !\n to loop through the measurements given
        {
            bloodData >> bpMeasurement;         //Extract a BP measurement
            bpTotal = bpMeasurement + bpTotal; //Add bpMeasurement to bpTotal
            bpIncrement++;                     //Increment number of blood pressure measurements
        }

        bpAvg = float(bpTotal) / float(bpIncrement);                        //Calculate the blood pressure average
        patientsBloodData << setw(5) << bpIncrement << setw(5) << bpAvg;    //Output and format the # of measurements
                                                                            //and blood pressure average
    }

2 个答案:

答案 0 :(得分:1)

std::getline读取整行然后还包括循环检查中的流读取会好得多。

这是一个经过修改的程序,用于演示:

#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

using std::setw;

auto data{R"(4122 90 78 50
3123 110 92 83 95 77
3183 123 94 97 99)"};

int main()
{
    std::istringstream bloodData{data};
    auto& patientsBloodData = std::cout;

    //while(bloodData.good())                      //Need to write while !EOF (changed: while True)

    for(std::string line; std::getline(bloodData, line);)
    {
        int patientID{};
        int bpMeasurement{};
        int bpTotal{};
        int bpIncrement{};
        float bpAvg{};
        std::istringstream bloodData_row{line};
        bloodData_row >> patientID;                 //Get patient ID (stop at first whitespace)
        patientsBloodData << patientID;         //Output patients ID

        //while(getchar() != '\n')                //Need to write while !\n to loop through the measurements given
        while(bloodData_row >> bpMeasurement)
        {
            //bloodData >> bpMeasurement;         //Extract a BP measurement
            bpTotal = bpMeasurement + bpTotal; //Add bpMeasurement to bpTotal
            bpIncrement++;                     //Increment number of blood pressure measurements
        }

        bpAvg = float(bpTotal) / float(bpIncrement);                        //Calculate the blood pressure average
        patientsBloodData << setw(5) << bpIncrement << setw(10) << bpAvg << '\n';    //Output and format the # of measurements
                                                                            //and blood pressure average
    }
}

输出:

4122    3   72.6667
3123    5      91.4
3183    4    103.25

答案 1 :(得分:0)

要从数据文件中提取数据,您可以简单地(在.cpp文件中):

while(cin >> patientID) {
}

编译代码并获取可执行文件后,您可以将文件bloodData.txt作为直接流发送到您的代码(在CLI中):

./a.out < bloodData.txt

这会将bloodData.txt的所有内容发送到cin,cin会将内容发送到变量PatientID,直到它到达EOF字符,导致bool返回while语句的false。