ofstream.is_open() always false in DirectX12 application using VS 2015

admin

Administrator
Staff member
I've been trying for hours now, and I can't for the life of me get my <strong>DirectX12</strong> application to write a simple file...

A bit about my setup:

<ul>
<li>Windows 10 updated. </li>
<li>DirectX12 default "spinning cube" application. DirectX 12 App (Universal Windows)</li>
<li>Visual Studio 2015</li>
</ul>

I am doing this:

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

What I have tried (almost everything under the sun and the kitchen sink):

<ul>
<li>I've also tried using
Code:
fstream
,
Code:
wfstream
as well as doing
Code:
!outfile.fail()
</li>
<li>I've checked <strong>every</strong> directory in my project, and even ventured out into the Microsoft DirectX SDK. </li>
<li>I've tried relative paths:
Code:
outFile.open("WHERETHEFISMYFILE.txt");
</li>
<li>I've tried setting an absolute path. </li>
<li>I've tried adding permissions to the folder by allowing "everyone" and giving full access - just for sanity. </li>
<li>I've also tried getting the current working directory, which is
Code:
C:\Users\pookie\Documents\Visual Studio 2015\Projects\demoDX12\x64\Debug\demoDX12\AppX
and setting it to
Code:
c:\
</li>
<li>I have created the file manually, in every folder of my project...</li>
<li>I've tried in both Debug and Release configs, as well as x86 and x64 and all possible combinations thereof</li>
<li>I've tried
Code:
\\
as well as
Code:
//
in my file path</li>
<li>I've tried replacing spaces in path with %20</li>
<li>I have also tried running Visual Studio in admin mode.</li>
</ul>

The problem occurs here:
Code:
if (outFile.is_open())
. For some reason, it always returns false.

What am I doing wrong here?

<strong>UPDATE:</strong>
To rest my mind, I tried an empty console application with the following code:

Code:
#include &lt;fstream&gt;
#include &lt;iostream&gt;

using namespace std;

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

The result is the same:
Code:
is_open() == false
. I'm at a loss here guys.

<strong>Update 2:</strong>

As requested, I am updating this question to show the exact project I am working with. I am working with the default DirectX12 application - the spinning cube. I <a href="https://digitalerr0r.wordpress.com/2015/08/19/quickstart-directx-12-programming/" rel="nofollow noreferrer">followed this tutorial</a>

Within my project, there is a method called
Code:
void DX::DeviceResources::Present()
and it is within this method that I am trying to write to file (although I have tried this in numerous other places within this project, too.

Here it is:

Code:
// 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-&gt;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 &lt;&lt; "Writing this to a file.\n";
            outFile.close();
        }
        else
        {
            cout &lt;&lt; "sdsadsdsd";
        }
    }
    catch (const std::exception&amp; ex)
    {
        cout &lt;&lt; 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();
    }
}

<strong>Update 3</strong>

So, a blank project with file output works fine, if I use

Code:
name("c:\\Users\\pookie\\Documents\\WHERETHEFISMYFILE.txt");
outFile.open(name.c_str(), ios_base::out);

Note the
Code:
ios_base::out

This is fine. However, this does still not work in the default DirectX12 application.

This is definitely an DirectX related issue. See <a href="https://stackoverflow.com/questions/25571859/opening-or-creating-a-file-from-a-directx-11-2-app">this</a>. I have tried doing as the solution in that post suggested, but I can still not get it to work.

I can also confirm that a brand new DirectX12 project has the same issue. Try it.

<strong>SOLUTION</strong>

Thanks to ebyrob, I have got this working. It turns out that these new Windows Apps can only write to certain folders... More specifically, this:

Code:
auto platformPath = ApplicationData::Current-&gt;RoamingFolder-&gt;Path;

Unfortunately, the path is not a standard string... so it must be converted first:

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

Then just add the file name:

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

and finally:

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

Thank you ebyrob!!