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

时间:2014-04-06 20:17:36

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

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

2 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:0)

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

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

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

如果p在开始时小于3599,那么它根本就不会运行。