gpt4 book ai didi

c++ - ofstream 不会在文档文件夹中创建文件

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

我正在尝试为我正在制作的游戏制作一个功能,将游戏中的数据保存到文件夹中的文本文件中,两者都具有用户提供的名称。我能够在我的项目文件夹中执行此操作,并希望它位于更通用的位置,所以我正在尝试文档文件夹。

但是,当我切换位置时,​​代码停止生成所需的结果,并开始在程序的主目录中创建文件和文件夹。 (文件不在文件夹内,仅供引用)

void Player::save()
{
system("mkdir \"C:\\Users\\Default\\Documents\\Ice Road\"");
//Make "Ice Road" folder in documents
std::string filename((name + ".txt"));
//make a name (inputed by the user earlier) to later be used to name a text file

std::string command("mkdir ");
//string to be combined with name to make folder
std::string commandString((command + name));
//combine to make string command that creates folder
std::string newDir = ("C:\\Users\\Default\\Documents\\Ice Road\\" + name);
//string to set directory to newly created folder

std::ofstream saveStream;
//open output stream for the saving process
SetCurrentDirectory("C:\\Users\\Default\\Documents\\Ice Road\\");
//set the directory to the Ice Road documents folder (DOES NOT WORK)
system((commandString.c_str()));
//create named folder for the save files.

SetCurrentDirectory(newDir.c_str());
//set the directory to the newly created folder
saveStream.open(filename.c_str());
//Create/open a text file that holds the data being saved
system("echo on");
//turn on echo for debugging

saveStream << name << std::endl
<< difficulty << std::endl
<< health << std::endl
<< warmth << std::endl
<< hunger << std::endl
<< packSpace << std::endl
<< packUsed << std::endl;
saveStream.close();
//input data to save file

system("dir");
//show folder for debugging
system("PAUSE");
//wait for input
}

我如何获得此代码以在名为 Ice Road 的文档中创建一个文件夹,其中包含命名文件夹和命名文本文件?

(文档\冰路\你的名字\你的名字.txt)

最佳答案

我解决了这个问题。我最大的问题是我无法访问此文件夹 (UAC),但因为我没有收到任何错误,所以我没有想到它。在那和其他一些调整之间,我让它工作如下所示,以管理员身份运行。

void Player::save()
{
std::string iceroad("C:\\Users\\Default\\Documents\\Ice Road");
//Ice road directory, made into a string variable for easy usage, as recommended by Ben
system("mkdir \"C:\\Users\\Default\\Documents\\Ice Road\"");
//Make Ice Road folder in documents
std::string filename(iceroad + "\\" + name + "\\" + name + ".txt");
//make a name (inputed by the user earlier) to later be used to name a text file, now using full address

std::string command("mkdir ");
//string to be combined with name to make folder
std::string commandString((command + name));
//combine to make string command that creates folder
std::string newDir = (iceroad + "\\" + name);
//string to set directory to newly created folder, simplified

std::cout << filename;
//debugging, as reccommended by Dietmar
std::ofstream saveStream;
//open output stream for the saving process
SetCurrentDirectory(iceroad.c_str());
//set the directory to the Ice Road documents folder
system("dir");
//debugging, as recommended by Dietmar
system((commandString.c_str()));
//create named folder for the save files.

SetCurrentDirectory(newDir.c_str());
//set the directory to the newly created folder
saveStream.open(filename.c_str());
//Create/open a text file that holds the data being saved
system("echo on");
//turn on echo for debugging

saveStream << name << std::endl
<< difficulty << std::endl
<< health << std::endl
<< warmth << std::endl
<< hunger << std::endl
<< packSpace << std::endl
<< packUsed << std::endl;
saveStream.close();
//inputs data to save file

system("dir");
//show folder for debugging
system("PAUSE");
//wait for input
}

感谢您的建设性批评,我确实将它们铭记在心并付诸实现。

关于c++ - ofstream 不会在文档文件夹中创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18477454/

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