我想要两个指向C++中相同文件的指针。多输出指针指向C++中的文件
#include <fstream>
int main()
{
double a;
std::ofstream fout1("out.txt");
fout1 << "CASE A:"<<std::endl;
std::ofstream fout2("out.txt", std::fstream::app);
fout2 << "CASE B:" <<std::endl;
for (int i=0; i < 3; i++){
a = i*i;
fout1 << a << std::endl;
fout2 << 10+a <<std::endl;
}
return 0;
}
此代码给我:
CASE A:
0
1
4
11
14
但我想:
CASE A:
0
1
4
CASE B:
10
11
14
在我真正的代码变量 “a” 是一个庞大的计算,所以不会做一个双是一个好的解决方案。我想到了两种解决方案:
- 使用double在矢量中保存“a”
- 使用两个指针指向同一个文件(当前问题);
我觉得2是最好的,所以我怎样才能使用两个指针到同一个文件?
你想“0”之前写的“情况B”中,但之后出现*。*“0”的文件,是正确的? – Beta