【发布时间】:2016-05-10 06:55:27
【问题描述】:
我已经尝试了好几个小时,但我无法让我的 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();
}
我尝试过的(几乎所有在阳光下和厨房水槽下的东西):
- 我也尝试过使用
fstream、wfstream以及!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();
}
谢谢你!
【问题讨论】:
-
如果将每个
//替换为/会发生什么? -
你有没有看这个问题:stackoverflow.com/questions/27075930/…
-
@pookie 好吧,您在示例中显示的
//绝对是错误的。而且跟direct-x没关系。 -
不幸的是,我被 Windows 困住了,真的,我不应该仅仅为了写入文本文件而更换操作系统;)
-
@AnmolSinghJaggi:由于
perror是一个 POSIX 函数,我认为我们不应该对它无法在 Windows 10 上编译感到完全惊讶。
标签: c++ ofstream directx-12