我試圖寫入文件,但只有最後一行被寫入文件。我試過打開和關閉循環外的文件,但是沒有任何文件寫入文件當寫入文件時,只有最後一行保存到我的輸出文件
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();
}
這就是完整的代碼。我試着拿出來放回去(打開和關閉文件)。目前爲止沒有任何工作
嘗試挑選幾個遠程正確的標籤。 – Joe
'for'循環沒什麼意義。它將運行 - 最多 - 運行一次(如果開始時爲'p> = 3599')。最後的'p = 0'線確保它永遠不會運行多次。 –
它會再次運行,因爲我將p設回零。代碼很好,我做了大量的變量測試。它只是沒有寫入文件的所有行 – user3504476