c++ - 写入文件时,只有最后一行保存到我的输出文件

标签 c++

我正在尝试写入文件,但只有最后一行被写入文件。我试过在循环外打开和关闭文件,但没有任何内容写入文件

void getValues (double totalEnergy, double meanPowerConsumption, double maxPowerConsumption);

int main()
{
    double totalEnergy, meanPowerConsumption, maxPowerConsumption;
    getValues(totalEnergy, meanPowerConsumption, maxPowerConsumption);

    return 0;
}

void getValues(double totalEnergy, double meanPowerConsumption, double maxPowerConsumption)
{
    int x = 0;
    int c = 0;
    double p = 0;
    int i = 0;
    ifstream inFile;
    inFile.open("data.txt");

    if (inFile.fail())
    {   cerr << "Error opening file." << endl;
        exit(1);
    }

    // Declaring variables.
    double power1, power2, time1, time2, totalPower, timeConstant, changeInPower, totalTime, time, coloumns;
    double year, month, day, hour, minute, second, voltage, current, frequency;
    double accumulatedPower=0;

    while(!inFile.eof())
    {
        inFile >> year >> month >> day >> hour >> minute >> second >> voltage >> current >> frequency;

        //Should have taken into account 'Years','Months' and 'Days' but its throws the calculations into exponents.
        time2 = ((3600*hour) + (minute *60) + second);

        if (x==0)
        {
            timeConstant = 0;
            time1 = 0;
            totalTime = 0;
        }
        else
        {
            timeConstant = time2 - time1;
            totalTime = totalTime + timeConstant;
        }

        //cout << "time1: " << time1 << endl;
        //cout << "time2: " << time2 << endl;
        //cout << "Time Constant: " << timeConstant<< endl;
        //cout << "Total Time" << totalTime << endl;

        power2 = voltage*current;

        if (x==0)
        {
            power1 = 0;
            changeInPower = 0;
            totalPower = 0;
            totalEnergy = 0;
            meanPowerConsumption = 0;
        }
        else
        {
            changeInPower = (power1 + power2)/2;
            totalPower = totalPower + changeInPower;
        }

        // cout << "Counter" << c << endl;
        // Assumed that mean powerconsumption is the average of all powers entered.
        meanPowerConsumption = totalPower / c;

        // Testing Variables.
        //cout << "power1: " << power1 << endl;
        //cout << "power2: " << power2 << endl;
        //cout << "Change in Power: " << changeInPower << endl;
        //cout << "total Power: " << totalPower << endl;


        //Numerical Integration:
        totalEnergy = totalEnergy + (timeConstant*changeInPower);

        //Counter Loop:

        if (power2 > maxPowerConsumption)
        {
            maxPowerConsumption = power2;
        }


        accumulatedPower = accumulatedPower + power1;
        time = time2 - time1;
        p = p + time;


        ofstream outFile;
        outFile.open("byhour.txt");

        for (coloumns=0; p>=3599; coloumns++)
        {
            i++;
            outFile << i << " " << accumulatedPower/3600000 << endl;

            accumulatedPower=0;
            p=0;
        }

        outFile.close();

        cout << "coloumns: " << i  << endl;
        cout << "P value " << p << endl;
        cout << "accumulated power" << accumulatedPower << endl;

        cout << "The total Energy is: " << totalEnergy/3600000 << "KwH" << endl;
        cout << "The mean power consumption is: " << meanPowerConsumption << endl;
        cout << "The Max Power Consumption is:" << maxPowerConsumption << endl;
        cout << endl ;

        c++;
        x++;

        time1 = time2;
        power1 = power2;
    }

    ofstream outStats;
    outStats.open("stats.txt");

    outStats << totalEnergy/3600000 << endl;
    outStats << meanPowerConsumption << endl;
    outStats << maxPowerConsumption << endl;

    outStats.close();
}

这是完整的代码。我尝试将其取出并放回原处(打开和关闭文件)。到目前为止没有任何效果

最佳答案

您正在循环打开和关闭文件;基于默认模式,它打开到文件中的第一个位置,所以每次写入您都打开文件,写入文件的开头(可能覆盖之前的内容),然后关闭它。

你应该打开文件一次,在循环中写出,然后在循环外关闭。

关于c++ - 写入文件时,只有最后一行保存到我的输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22899468/

相关文章:

c++ - 我的 Qt 程序显示带有标题的空白窗口

C++ 动态分配的内存返回一个数组

c++ - 用于在函数上插入值的 cin 命令

c++ - 定点数的动态格式化

c++ - sem_wating 线程是否会导致更多切换

c++ - 未命中断点 - "the module did not load at the default load address"

c++ - 通过类存储和更新 vector 中的当前最小值

c++ - 尝试使用模板创建类的新实例,出现意外错误

c++ - C++编程实践与异常处理的混淆

c++ - C/C++ 有哪些突变测试框架?