- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已经尝试了几个小时了,但我终究无法让我的 DirectX12 应用程序编写一个简单的文件...
关于我的设置的一些信息:
我这样做:
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()
outFile.open("WHERETHEFISMYFILE.txt");
C:\Users\pookie\Documents\Visual Studio 2015\Projects\demoDX12\x64\Debug\demoDX12\AppX
并进行设置到 c:\
\\
以及 //
问题出现在这里: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/
我想在一个函数中打开一个文件,将打开的文件对象返回给 main,并在另一个函数中使用它函数来填充文件。编译器似乎告诉我,我正在尝试访问 iostream 的私有(private)成员。有没有办法做到这
我最近写了一个记录器类。编译这段代码时: std::ofstream *stream = new std::ofstream; stream->open( log_file_name.c_str()
我有以下测试代码,其中有一个参数 fS,它是 ofstream 的容器: #include #include #include #include int
我正在尝试实现一个记录器,它可以注册到多个流,如 ostringstream、ofstream 等。我试图实现这样的功能 void register_stream(std::ostream& a);
我有一个在线程上写入文件的类 class A{ public: void writeToFile(ofstream& outFile, obj &a) { //...
我有以下代码 void _log_message(const char* fmt, ...) { va_list arg; ofstream logfile; cout << "Open Lo
我刚刚开始阅读如何打开和编辑文件。使用 ifstream 时,如果文件不存在,则不会创建它。 引用下面的代码,条件 (!outfile) 何时为假,就好像文件不存在一样,构造函数将简单地创建它,因此总
#include #define LOGFATAL(msg) log(0, msg) std::ofstream *logst = NULL; void log(int sev, char *msg
我在使用 ofstream 时遇到问题一次将两个不同的输出写入两个不同的文件。程序编译运行正常,并向p1output.txt写入数据,但是当我打开p2output.txt时,除了第一行内容外是空白的:
我有一个“资源模块”类来管理我的应用程序的所有资源,它主要从一个包含所有内容的文件“RESOURCES.DAT”中读取。 从文件中请求新数据的所有对象都通过 ResourceModule,因此我可以管
我遇到流问题,程序无法打开我要写入的文件。最小完整可变代码如下: #include #include using namespace std; int main(){ string roo
我已经研究了好几个小时了,我只知道答案很简单。似乎无论我做什么我都无法打开文件。这是一个多类程序,所以在标题中我有 #include #include class A{ string path
所以我想写一个可以像这样使用的缩进输出类: Debug f; f.open("test.txt"); f }' and '') ofs #include using namespace std;
大家好... 抱歉我的英语不好,但是会说西类牙语... 这周,为了这个proyect的学习和工作,我想创建一个软件来制作文件(.us)... 示例 char name[50]; //Or string
我不想在 main() 中构造 ofstream。这是我所做的,但它没有编译: #include using namespace std; class test { private: ofs
谁能告诉我这是怎么回事? #include #include #include #include #include class writeManager { std::vector
我有一个 C++ 类,它将其数据写出到二进制 std::ofstream。该类将数据存储为 boost:shared_array 但我已经消除了这个问题。问题出在 ofstream 上对 write(
在我的程序中,我的 dataout: void outfile 没有写入文件,任何人都可以找出原因吗? using namespace std; #include #include #include
我想在文件上写点东西。但它只是没有像它必须假设的那样编写。 void main() { int accno; char name[20]; float deposit;
我遇到了一个奇怪的问题。我有两个函数,它们都有一个通过引用传递的 ofstream。但是,当我调用第二个函数时,正在打印第一个函数的一部分。 这是第一个函数: void GamePlay::dealD
我是一名优秀的程序员,十分优秀!