gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:41:05 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com