gpt4 book ai didi

c++ - 更新文件时如何防止结构成员值被覆盖 C++

转载 作者:行者123 更新时间:2023-11-28 03:24:57 25 4
gpt4 key购买 nike

假设我有一个二进制文件、一个文本文件和 3 个结构成员。我已经为结构成员 1 写了一组数字,我们称它为“score1”。现在,我想用结构成员 2“score final”更新二进制文件。

例如,这个分数最终将计算分数 1 的百分比并将其写入二进制文件。现在我们将写入 score2 的第二组值。当我这样做时,score1 值以及二进制文件中的原始分数最终值都消失了,现在我只有分数 2 和从 score2 计算的新分数最终值。

我的代码示例:

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

正在读取的文本文件将如下所示;

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

然后我会将结构 score1 和 score2 以及 final 输出到如下所示的文本文件中

Final   Score1   Score2 
12.2 76.1
41.2 5.7
51.5 62.3
56.2 52.7
9.2 2.2

最后一栏是空的,但你明白我的意思了。

现在的问题:

  1. Everytime I output it to the textfile, I can either only do final column, score1, OR final column score2. But not score1, score2, final.

  2. 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.

现在这是一项任务,我有一些限制,我必须完成的任务贴近。

Rules: Read in score1 and score2 from textfile. Use binary to store score1, score2, final. Write to a single textfile with these three columns.

最佳答案

那是不可能的。 IO 流类允许您将数据附加到现有文件或截断它并从头开始重写。

在您的情况下,append 不起作用。所以你剩下的就是截断并重写文件中你想要的所有信息。

关于c++ - 更新文件时如何防止结构成员值被覆盖 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14360315/

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