Reputation: 27
Let's say I have a binary file, a text file, and 3 struct members. I already wrote in a set of numbers for struct member 1, lets call this one "score1". Now, I want to update the binary file with struct member 2, "score final".
This score final will for instance, calculate score1's percentage and write it to the binary file. Now we'll write in the 2nd set of values for score2. When I do that, score1 values are gone as well as my original score final value from the binary file and now I only have score 2 and the new score final calculated from score2.
sample of my code:
struct Scores{
float score1;
float score2;
float final;
};
fstream afile;
fstream afile2;
//afile will read in sets of score1 values from text file
//afile2 will output sets of score1 values to binary file
//while final is also outputted.
//Then, afile will again read in sets different of score2 values from text file
//afile 2 will output sets of score1 values to binary file
//and final is also outputted but with new calculations
Text file being read from will look like this;
12.2
41.2
51.5
56.2
9.2
and the second text file:
76.1
5.7
62.3
52.7
2.2
And I will output the struct score1 and score2 and final to a text file that looks like this
Final Score1 Score2
12.2 76.1
41.2 5.7
51.5 62.3
56.2 52.7
9.2 2.2
The final column is empty but you get my point.
Now the issues:
Everytime I output it to the textfile, I can either only do final column, score1, OR final column score2. But not score1, score2, final.
I want to be able to add the result of the final from score1 and add up with the final from the score2 and output the addition of two finals.
Now as this is an assignment, I have limitations, tasks that I have to stick close to.
Rules: Read in score1 and score2 from textfile. Use binary to store score1, score2, final. Write to a single textfile with these three columns.
Upvotes: 0
Views: 318
Reputation: 51
If your binary file is a simple list of struct Scores objects you can implement two very simple functions to modify binary (didn't check errors, if it compiles etc. - do it yourself).
Scores readScores(std::ifstream& file, unsigned int scoresNum)
{
Scores scores;
file.seekg(scoresNum*sizeof(Scores), std::ios_base::beg);
file.read(static_cast<char*>(&scores),sizeof(Scores));
return scores;
}
void writeScores(std::ofstream& file, unsigned int scoresNum, const Scores& scores)
{
file.seekp(scoresNum*sizeof(Scores), std::ios_base::beg);
file.write(static_cast<char*>(&scores),sizeof(Scores));
}
You load first text file, modify binary. Then second file, modifying once again and generate a result based on final state of binary file. I hope it will help you solve the problem.
Upvotes: 0
Reputation: 361762
That is not possible. The IO stream classes let you either append data to the existing file OR truncate it and rewrite all from the beginning.
In your case, append wouldn't work. So what you're left with it is truncate and rewrite all the information you want in the file.
Upvotes: 2