2013-08-28 278 views
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)

+1

我會嘗試使用''C:\\ Users \\ Default \\ Documents \\ Ice Road \\「+ name +」.txt「'作爲'filename'。 –

+0

@Dietmar:這是我列表中的#2,#1將該目錄名稱放入一個變量並重新使用它,以確保所有路徑實際上是相同的名稱。 –

+0

到目前爲止沒有效果。 –

回答

0

我解決了這個問題。我最大的問題是我無法訪問此文件夾(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 
} 

感謝您的建設性批評,我確實把他們放在心上並加以實施。

相關問題