gpt4 book ai didi

c++ - 将 const char * 传递给 file.open(),奇怪的行为

转载 作者:太空宇宙 更新时间:2023-11-04 16:02:41 24 4
gpt4 key购买 nike

<分区>

我对下面的代码有疑问。根据我使用的 IDE,我遇到了不同的行为。

Dev-C++: 运行良好。但是,如果我将 GenerateFileName(0,0) 传递给 file.open(),则不会创建任何文件。

Visual Studio 2013:在所有情况下都运行良好,但是,生成的文件名称看起来像

ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌPùD

或类似的东西,文件本身没有扩展名(我希望是 .txt 文件)。

int main()
{
ofstream file;
file.open(GenerateFileName(3, 0));
file << 1 << endl;
file.close();
_getch();
}

const char* GenerateFileName(int Instance_nth, int Input_nth)
{
string filename = to_string(Instance_nth);
filename += "_";
filename += to_string(Input_nth);
filename += ".txt";

return filename.c_str();
}

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