gpt4 book ai didi

c++ - 如何在 C++ 中将 int 连接到 wchar_t*?

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

我要创建和写入N个文件,每个文件都必须以整数结尾来标识它。

这是我的一段代码:

for(int i=0; i<MAX; i++)
{
uscita.open("nameFile"+i+".txt", ios::out);
uscita << getData() << endl;
uscita.close();
}

这就是我想在执行后在我的目录中找到的内容:

nameFile0.txt
nameFile1.txt
nameFile2.txt
...
nameFileMAX.txt

上面代码的问题是我得到了编译错误:

error C2110: '+' Impossible to add two pointers

如果我尝试为名称创建一个字符串,则会出现另一个问题:

string s ="nameFile"+i+".txt";
uscita.open(s, ios::out);

问题是:

error C2664: you cannot convert from string to const wchar_t*

我能做什么?如何创建具有不同名称的文件,将 int 连接到 wchar_t*

最佳答案

你可以使用std::to_wstring:

#include <string>

// ...

std::wstring s = std::wstring("file_") + std::to_wstring(i) + std::wstring(".dat");

(如果您需要 C 风格的 wchar_t*,则使用 s.c_str()。)

关于c++ - 如何在 C++ 中将 int 连接到 wchar_t*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8609842/

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