gpt4 book ai didi

c++ - 更改 ofstream 文件 C++ 的位置时出错

转载 作者:行者123 更新时间:2023-11-28 00:45:50 26 4
gpt4 key购买 nike

我的代码看起来像这样:

std::vector <std::string> info;
info.push_back("10");
info.push_back("this is a line to print");
std::ofstream myfile;
myfile.open("list.txt");
myfile<<"first*"<<info[0]<<"\n";

它工作正常,但是当它创建文件并打印信息时,但是当我将其更改为:

std::vector <std::string> info;
info.push_back("10");
info.push_back("this is a line to print");
std::ofstream myfile;
myfile.open("Output\\list.txt");
myfile<<"first*"<<info[0]<<"\n";

它创建了文件,但当我尝试输出到文件时,整个过程崩溃了。我在 Windows 8 上使用带有 GNU gcc 编译器的 code::blocks。

最佳答案

将绝对路径提供为“C:\Documents\yourprojects\yourcurrentproject\Output\list.txt”。它会工作正常。我尝试过这个。

关于c++ - 更改 ofstream 文件 C++ 的位置时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16283304/

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