问题
I'm using direct3D 11.1 to create my game,i want to store player last record in a file so here is my code:(it fails in !File.good() )
fstream File("Score.txt");
if (!File.good())
{
return;
}
if (!File.is_open())
{
return;
}
if (File.bad())
{
return;
}
Writer.Write("Easy", getString(Scene->Score), File);
File.close();
and here is my XMLWriter class to write in file:
class XmlWriter
{
public:
void Write(string const &replace, string const &replaceBy, fstream &file)
{
fstream tempFile;
tempFile.open("ScoreT.xml", fstream::out | fstream::trunc | ios::app);
if (!tempFile)
{
return;
}
string curLine = "";
while (!file.eof())
{
getline(file, curLine);
if (findWord(replace, curLine))
{
int j = 0;
while (curLine[j] != replace[0])
{
j++;
}
j += replace.size() + 1;
for (int k = 0; k < replaceBy.size(); k++)
{
curLine[j] = replaceBy[k];
j++;
}
if (curLine[j] != ',' && curLine[j] != '<')
{
curLine[j] = ' ';
}
tempFile << curLine << "\n";
}
else
{
string temp = "";
file << temp;
tempFile << curLine << "\n";
}
}
file.close();
tempFile.close();
if (remove("Setting.xml") != 0) perror("Error removing file!");
rename("ScoreT.xml", "Score.xml");
}
private:
bool findWord(string search, string searchLine)
{
bool ret = false;
for (int i = 0, j = 0; i < searchLine.size() && j<search.size(); i++)
{
if (search[j] == searchLine[i])
{
ret = true;
j++;
continue;
}
ret = false;
j = 0;
}
return ret;
}
};
but any time i run it,it can't create a new file and fail! what's the problem with it? Note that i do this in my XAML code.
回答1:
Store apps are only permitted write access to a handful of file paths. By calling fstream::open
without a fully qualified path, the API uses the working directory, which in the case of a store app is always the app package path (a path which the app does not have write access to).
The solution is to use a fully qualified path that the app has write access to. For saving player data, your best bet is to use the ApplicationData::Current->RoamingFolder
folder, use the Path
property to get the full path, and append the filename you want to the string. Then pass the fully qualified file path string to fstream::open
and it should succeed.
来源:https://stackoverflow.com/questions/25815285/cant-create-file-using-fstream-in-windows-store-app-8-1