1
我正在嘗試爲我製作的遊戲製作一個功能,該功能將遊戲中的數據保存到文件夾中的文本文件中,同時使用用戶提供的名稱。我能夠在我的項目文件夾中做到這一點,並希望它在更普遍的地方,所以我正在嘗試文檔文件夾。ofstream不會在文檔文件夾中創建文件
但是,當我切換位置時,代碼停止生成所需的結果,並開始在我的程序主目錄中創建文件和文件夾。 (該文件不是在文件夾中,供參考)
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
}
我怎麼能得到這個代碼中創建一個名爲雪道文件的文件夾,用命名的文件夾內,裏面已命名的文本文件?
(文檔\雪道\提供yourname \ yourname.txt)
我會嘗試使用''C:\\ Users \\ Default \\ Documents \\ Ice Road \\「+ name +」.txt「'作爲'filename'。 –
@Dietmar:這是我列表中的#2,#1將該目錄名稱放入一個變量並重新使用它,以確保所有路徑實際上是相同的名稱。 –
到目前爲止沒有效果。 –