c++ - ofstream.is_open() 在使用 VS 2015 的 DirectX12 应用程序中始终为 false

标签 c++ ofstream directx-12

我已经尝试了几个小时了,但我终究无法让我的 DirectX12 应用程序编写一个简单的文件...

关于我的设置的一些信息:

  • 更新了 Windows 10。
  • DirectX12 默认的“旋转立方体”应用程序。 DirectX 12 应用程序(通用 Windows)
  • Visual Studio 2015

我这样做:

ofstream outFile;
// I have even tried with \\ slashes...
outFile.open("c://Users//pookie//Documents//WHERETHEFISMYFILE.txt");
if (outFile.is_open())
{
    outFile << "Writing this to a file.\n";
    outFile.close();
}

我尝试过的(几乎所有在阳光下和厨房水槽下的东西):

  • 我也尝试过使用 fstreamwfstream 以及 !outfile.fail()
  • 我已经检查了项目中的每个 目录,甚至冒险进入了 Microsoft DirectX SDK。
  • 我试过相对路径:outFile.open("WHERETHEFISMYFILE.txt");
  • 我试过设置绝对路径。
  • 我已经尝试通过允许“所有人”并授予完全访问权限来向文件夹添加权限 - 只是为了理智。
  • 我还尝试获取当前工作目录,即 C:\Users\pookie\Documents\Visual Studio 2015\Projects\demoDX12\x64\Debug\demoDX12\AppX 并进行设置到 c:\
  • 我已经在项目的每个文件夹中手动创建了文件...
  • 我已经尝试过调试和发布配置,以及 x86 和 x64 及其所有可能的组合
  • 我已经在我的文件路径中尝试了 \\ 以及 //
  • 我试过用 %20 替换路径中的空格
  • 我还尝试过在管理员模式下运行 Visual Studio。

问题出现在这里:if (outFile.is_open())。由于某种原因,它总是返回 false。

我在这里做错了什么?

更新: 为了让我安心,我尝试了一个带有以下代码的空控制台应用程序:

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    try
    {
        wfstream  outFile;
        outFile.open("C:\\Users\\pookie\\Documents\\WHERETHEFISMYFILE.txt");
        if (outFile.is_open())
        {
            outFile << "Writing this to a file.\n";
            outFile.close();
        }
        else
        {
            cout << "sdsadsdsd";
        }
    }
    catch (const std::exception& ex)
    {
        cout << ex.what();
    }
    return 0;
}

结果是一样的:is_open() == false。伙计们,我在这里不知所措。

更新 2:

根据要求,我正在更新此问题以显示我正在处理的确切项目。我正在使用默认的 DirectX12 应用程序 - 旋转立方体。我 followed this tutorial

在我的项目中,有一个名为 void DX::DeviceResources::Present() 的方法,正是在这个方法中,我试图写入文件(尽管我已经在该项目中还有许多其他地方。

这里是:

// Present the contents of the swap chain to the screen.
void DX::DeviceResources::Present()
{
    // The first argument instructs DXGI to block until VSync, putting the application
    // to sleep until the next VSync. This ensures we don't waste any cycles rendering
    // frames that will never be displayed to the screen.
    HRESULT hr = m_swapChain->Present(1, 0);

    try
    {
        wfstream  outFile;
        std::string 
         //This has been done numerous ways, but ultimately, I believe that 
         //ios_base::out is required if the file does not yet exist.
        name("c:\\Users\\pookie\\Documents\\WHERETHEFISMYFILE.txt");
        outFile.open(name.c_str(), ios_base::out);
        if (outFile.is_open())
        {
            outFile << "Writing this to a file.\n";
            outFile.close();
        }
        else
        {
            cout << "sdsadsdsd";
        }
    }
    catch (const std::exception& ex)
    {
        cout << ex.what();
    }


    // If the device was removed either by a disconnection or a driver upgrade, we 
    // must recreate all device resources.
    if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET)
    {
        m_deviceRemoved = true;
    }
    else
    {
        DX::ThrowIfFailed(hr);

        MoveToNextFrame();
    }
}

更新 3

因此,如果我使用

,则带有文件输出的空白项目可以正常工作
name("c:\\Users\\pookie\\Documents\\WHERETHEFISMYFILE.txt");
outFile.open(name.c_str(), ios_base::out);

注意 ios_base::out

这很好。但是,这在默认的 DirectX12 应用程序中仍然不起作用。

这绝对是一个与 DirectX 相关的问题。参见 this .我已尝试按照该帖子中建议的解决方案进行操作,但仍然无法正常工作。

我也可以确认一个全新的 DirectX12 项目有同样的问题。试试吧。

解决方案

感谢 ebyrob,我已经开始工作了。事实证明,这些新的 Windows 应用程序只能写入某些文件夹……更具体地说,是这样的:

auto platformPath = ApplicationData::Current->RoamingFolder->Path;

不幸的是,路径不是标准字符串...所以必须先转换:

auto platformPath = ApplicationData::Current->RoamingFolder->Path;
std::wstring platformPathW(platformPath->Begin());
std::string convertedPlatformPath(platformPathW.begin(), platformPathW.end());

然后只需添加文件名:

std::string path = convertedPlatformPath + "\\WHERETHFISMYFILE.txt";

最后:

try
{
    wofstream  outFile;
    char buff[256]; 
    outFile.open(path.c_str(), ios_base::out);
    if (outFile.is_open())
    {
        outFile << "Writing this to a file.\n";
        outFile.close();
    }
    else
    {
        cout << "Cannot open file " << name << ": " << strerror_s(buff,0) << endl;
    }
}
catch (const std::exception& ex)
{
    cout << ex.what();
}

谢谢你!

最佳答案

看到这个答案:

std::fstream doesn't create file

您需要为 wfstream::open() 指定正确的选项才能创建新文件。没有 ::trunc::in 将需要一个现有文件。

例子:

outFile.open("C:\\Users\\pookie\\Documents\\WHERETHEFISMYFILE.txt", ios_base::out);

在这种情况下,似乎还有另一个问题。该应用程序是 Windows 10 应用程序商店的一部分,因此它只能访问几个位置的文件。

请查看此线程:Can't create file using fstream in windows store app 8.1

有关如何打开应用程序实际 有权访问的本地或漫游用户配置文件目录中的文件。

关于c++ - ofstream.is_open() 在使用 VS 2015 的 DirectX12 应用程序中始终为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35137043/

相关文章:

c++ - SQLite:错误:C1083:无法打开包含文件: 'sqlite3.h'

c++ - 使用 ofstream 的嵌入式函数

c++ - 'CommandList::DrawInstanced' 如何选择多个槽中的顶点缓冲区之一?

c++ - 你如何读取顶点/索引缓冲区?

c# - 将 Directx12 资源移植到 XAML 元素:关于 WRL::ComPtr 替换的建议?

c++ - 仅寻址 WORD 的 15 位

c++ - 有没有一种方法可以使用重载运算符作为比较的一部分

c++ - WinAPI : Change Color of a TreeView using CustomDraw

c++ - 函数不能正确计算文件中的整数?

c++ - 为什么 mkdir() 后跟 ofstream::operator<< 会因权限被拒绝而失败?