Hello atoken,
Sorry about missing the most important part as I was distracted and lost track of where I was at.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
std::fstream ttfile("Total Time.txt", ios::in | ios::out);
//checks if text file exists if not makes one and stores current total hours
if (ttfile)
{
ttfile.seekg(0);
ttfile >> rhours;
ttfile >> rminutes;
// Figure total hours needs changed.
// Total minutes also needs changed.
ttfile.seekg(0);
ttfile << Thours << " " << Tminutes;
}
else
{
//ttfile.clear(); // <--- Not needed. If you reach here the stream did not open on the first try, so opening the stream as
// an "ofstream" is not a problem.
std::ofstream ttfile("Total Time.txt");
ttfile << difference.hours << ' ' << difference.minutes; // <--- This is all you need. Note the space.
// <--- Do not see your point here because the if and else are exactly the same.
//if (difference.minutes < 10)
//{
// ttfile << difference.hours << ' ' << difference.minutes; // <--- Added the space.
//}
//else
//{
// ttfile << difference.hours << ' ' << difference.minutes; // <--- Added the space.
//}
}
ttfile.close();
|
Although
ios_base
works all you really need is
ios::
. No point in making extra work that you do not need.
I eventually figured how the if/else was working and that part does work.
When you open the "fstream" for output it put the file pointer at the end of the file ready to append the file. Even when you open the stream for input and output it still puts the file pointer at the end of the file.
The first line of the if section sets the file pointer to the beginning of the file so that you can read the two numbers in the file. Otherwise the file pointer is at the end of the file and the first read will set "eof" on the stream making it unusable and it will read or write nothing.
The two lines to figure total hours and minutes need to be done differently.
Hint: When figuring total hours you need to account for "rminutes" and "difference.minutes" being over 60. and when figuring total minutes you need what is left over or the
remainder.
When I first started testing the program total minutes increased to over 200. Not what you want.
When you write the numbers to the file at most you will have two digits for hours an minutes. Also note fot the "Total Time.txt" file leading zeros make no difference. When the file is read and the numbers stored into an "int" any leading zeros will be dropped.
Before you write to the file again you will have to set the file pointer to the beginning.
In my testing I found that "seekg" worked and that "seekp" did not work. Then again this may just be my computer or something I am missing as I do not have the greatest experience using a "fstream" for both input and output.
The comments in the else section should explain it.
Hope that helps,
Andy