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

When writing to a file, only the last line saves to my output file

本文关键字:文件 保存 一行 我的 输出 最后      更新时间:2023-10-16

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

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();
}

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

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

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

您的for循环实际上(最多)运行一次。

for (coloumns=0; p>=3599; coloumns++)
{
    ...
    p=0;
}

假设循环开始时p至少为 3599,则循环将只运行一次,因为循环结束时p设置为 0(意味着下次测试将为 false,循环停止)。

如果开始时p小于 3599,当然,它根本不会运行。