gpt4 book ai didi

c++ - 从不同的 cpp 文件写入输出文件

转载 作者:行者123 更新时间:2023-11-28 07:52:19 25 4
gpt4 key购买 nike

我们有三个不同的 .cpp 文件。当我在 random.cpp 中创建文件时

    ofstream outfile ("random.log");

然后我在 random.cpp 文件中写入 random.log 文件:

    outfile << " something" ;

然后我转到一个不同的 .cpp 文件,例如 StudentCS.cpp,我使用以下命令打开该文件:

    ofstream outfile;
outfile.open("random.log",std::ios_base::app);

它会在所有 random.cpp 输出之后写入所有 StudentCS 输出,即使我在 random.cpp 中间调用 StudentCS。我正在尝试从 random.cpp 开始写,然后它调用 StudentCS.cpp,它应该写一些东西然后返回到 random.cpp,它再次写入

最佳答案

打开文件一次,然后传递流对象。或者更好:使记录器对象可用于任何需要记录的模块。

关于c++ - 从不同的 cpp 文件写入输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13517996/

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