c++ - When writing to a file, only the last line saves to my output file -
im trying write file last line gets written file. i've tried opening , closing file outside loop nothing writes file
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 = 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 account 'years','months' , 'days' throws calculations 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 mean powerconsumption average of 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"); (coloumns=0; p>=3599; coloumns++) { i++; outfile << << " " << accumulatedpower/3600000 << endl; accumulatedpower=0; p=0; } outfile.close(); cout << "coloumns: " << << 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(); }
that's full code. tried taking out , putting in (open , close file). nothing has worked far
you opening , closing file in loop; based on default mode it's opening first position in file, each write opening file, writing start of file (likely overwriting there before), , closing it.
you should open file once, write out in loop, , close outside loop.
Comments
Post a Comment