【问题标题】:Can't create file using fstream in windows store app 8.1无法在 Windows 商店应用程序 8.1 中使用 fstream 创建文件
【发布时间】:2014-09-12 19:16:32
【问题描述】:

我正在使用 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 代码中执行此操作。

【问题讨论】:

    标签: c++ windows-store-apps direct3d11


    【解决方案1】:

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

    解决方案是使用应用具有写入权限的完全限定路径。为了保存玩家数据,最好的办法是使用ApplicationData::Current-&gt;RoamingFolder 文件夹,使用Path 属性获取完整路径,并将您想要的文件名附加到字符串中。然后将完全限定的文件路径字符串传递给fstream::open,它应该会成功。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多