gpt4 book ai didi

c++ - 使用 stringstream 将 int 插入文件名

转载 作者:太空狗 更新时间:2023-10-29 21:19:50 25 4
gpt4 key购买 nike

我使用的是 c++ 98(因为这是安装在大学服务器上的)。我正在尝试将输出保存到文件中。文件名必须是 dice_N.dat,其中 N 是掷骰子的次数,我称之为 n_trials。我已尝试按照有关 inserting int variable in file name 的建议进行操作.使用第二个建议时,我得到输出 N.dat。这是我尝试执行此操作的代码片段:

    ostringstream fileNameStream("dice_");
fileNameStream << n_trials << ".dat";
string fileName = fileNameStream.str();
ofstream myfile;
myfile.open(fileName.c_str());

我不能使用 to_string 因为这在 c++ 98 中不受支持

最佳答案

使用:

ostringstream fileNameStream("dice_", std::ios_base::ate);

指定流打开模式为ate

参见 here

关于c++ - 使用 stringstream 将 int 插入文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26161072/

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