1

私が作成しているゲームの関数を作成しようとしています。これは、ゲームのデータをユーザーが指定した名前のフォルダー内のテキスト ファイルに保存します。私は自分のプロジェクト フォルダーでこれを行うことができましたが、より普遍的な場所にしたかったので、ドキュメント フォルダーを試しています。

しかし、場所を切り替えると、コードは目的の結果を生成しなくなり、プログラムのメイン ディレクトリにファイルとフォルダーの作成を開始しました。(ファイルはフォルダにありません、fyi)

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 という名前のドキュメント内にフォルダーを作成し、その中に名前付きのフォルダーとその中に名前付きのテキスト ファイルを作成するにはどうすればよいでしょうか?

(Documents\Ice Road\お名前\お名前.txt)

4

1 に答える 1

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
 }

あなたの建設的な批評をありがとう、私はそれらを心に留めて実行しました.

于 2013-08-28T00:57:10.243 に答える