gpt4 book ai didi

c++ - 无法输出到输出文件

转载 作者:行者123 更新时间:2023-11-28 01:49:13 24 4
gpt4 key购买 nike

<分区>

在 C++ 中,您可以输出到一个输出文件,然后再次重新打开输出文件以在原始输出下输出其他内容。

例如,我打开输出文件,然后输出标题,然后关闭输出文件。

然后重新打开输出文件并在该标题下输出一些数据,然后再次关闭它。

但是当我尝试这样做时,我的标题被删除并且它只输出数据

我会将标题输出到此处的输出文件:ID# NAME BALANCE

然后我关闭它,然后尝试输出它下面的数据。例如:1001 约翰尼 1050.00

它将覆盖标题并只输出数据

所以不要把它放在我的输出文件中

ID#   NAME    BALANCE
----------------------
1001 Johnny 1050.00

它只会在输出文件中有这个:

1001  Johnny 1050.00

这是我输出标题的函数:

void OutputHeaderToFile(string outFileName)
{
ofstream fout; /** allows the ability to use fout **/

fout.open(outFileName.c_str());

fout << "ID #" << left << setw(20) << "Name" << setw(21) << "BALANCE DUE" << endl;
fout << right;
fout << "----" << left << setw(20) << "--------------------" << setw(20) << "-----------"
<< endl;
fout.close();
}

它会输出

   ID #Name                BALANCE DUE          
-----------------------------------

然后我有另一个函数来输出该标题下的数据。

void OutputDataToFile(string nameArray[], int idArray[], float balArray[], string outFileName)
{
ofstream fout; /** allows the ability to use fout **/

fout.open(outFileName.c_str());
fout << idArray[searchIndex] << " " << nameArray[searchIndex] << " " << balArray[searchIndex];
fout.close();
}

我已经包括了#include <fstream>在我的头文件中。

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