无法在Windows Store应用程序8.1中使用fstream创建文件 [英] Can't create file using fstream in windows store app 8.1

查看:91
本文介绍了无法在Windows Store应用程序8.1中使用fstream创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Direct3D 11.1创建游戏,我想将玩家的最后记录存储在文件中,所以这是我的代码:(它在!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();

这是我要写入文件的XMLWriter类:

   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;
    }
};

但是任何时候我运行它,都无法创建新文件并失败! 有什么问题吗? 请注意,我是在我的XAML代码中执行此操作的.

解决方案

仅允许商店应用对少数文件路径进行写访问.通过在没有完全限定路径的情况下调用fstream::open,API将使用工作目录,在商店应用程序的情况下,该目录始终是应用程序包路径(该应用程序没有写权限的路径).

解决方案是使用应用程序具有写权限的标准路径.为了保存玩家数据,最好的选择是使用ApplicationData::Current->RoamingFolder文件夹,使用Path属性获取完整路径,然后将所需的文件名附加到字符串中.然后将完全限定的文件路径字符串传递给fstream::open,它将成功.

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.

解决方案

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.

这篇关于无法在Windows Store应用程序8.1中使用fstream创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆