1

I have created the a default DirectX12 application (the spinning 3D cube) and within my void DX::DeviceResources::Present() I am trying to write the backbuffer to a file:

// 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);

    UINT backBufferIndex = m_swapChain->GetCurrentBackBufferIndex();
    ComPtr<ID3D12Resource>         spBackBuffer;
    m_swapChain->GetBuffer(backBufferIndex, IID_PPV_ARGS(&spBackBuffer));

    //before writing the buffer, I want to check that the file is being 
    //created
    ofstream myfile;
    myfile.open("WHEREISTHEFILE.txt");
    myfile << "Writing this to a file.\n";
    myfile.close();

    // 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();
    }
}

The problem occurs here:

ofstream myfile;
myfile.open("WHEREISTHEFILE.txt");
myfile << "Writing this to a file.\n";
myfile.close();

I just want to write a file first (as illustrated how here), before trying to write the contents of the back buffer. Now, for some reason, I cannot find the output file... I have searched everywhere, all directories in my project and even in the Microsoft DirectX SDK folder.

There are no exceptions thrown and I can step through each line while debugging without error.

Where could it be?

pookie
  • 3,796
  • 6
  • 49
  • 105

2 Answers2

3

It should be in the directory of your project. If you are using Visual Studio, you can right-click your solution and click Open folder in File explorer.

Image: Open folder in File explorer

(I embedded it like this because I need 10 reputation to post an image directly)

Also with your code now, there is no way to determine whether your program is actually able to open the output file or not. I suggest you use something like this:

std::ofstream outputFile("./myOutput.txt");
if (outputFile.fail())
{
    std::cout << "Failed to open outputfile.\n";
}
outputFile << "I like trains.";
outputFile.close();

The first line is an initialisation that does the same as .open(). Also mind the "./" in front of the path, this should not be mandatory but it can't hurt to do this (this means your current directory). The important part about this piece of code is the .fail() check, if your program failed to open the outputfile, you will ofcourse not be able to find it anywhere. Hope this helped!

Julian Declercq
  • 1,536
  • 3
  • 17
  • 32
  • Thanks, but I have since discovered that outputFile.is_open is always false, so for some reason, I am unable to even create the file! ;) – pookie Feb 01 '16 at 13:10
  • Aah I just saw you reply on the other response while I was trying it out myself in Visual studio. Have you tried making the .txt file manually in your working directory first and then (in code) open it and write to it? – Julian Declercq Feb 01 '16 at 13:12
  • Yeah, tried that ;) no luck... still can't open file or write to one! – pookie Feb 01 '16 at 13:16
2

Where could it be?

Usually the file location is relative to your current working directory, i.e. WHEREISTHEFILE.txt should be located in whatever directory you were in when you started the program.

You can determine that directory inside your program via GetCurrentDirectory(), and change it to something else via SetCurrentDirectory().

But you did not check if the .open() was successful, so the writing could have failed altogether, for example due to insufficient permissions...?!

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • Thanks. I checked to see if ofstream.is_open == false, and it is... I am running in administrator mode, have added permissions to the directory manually by giving full control to 'everyone', but it is_open is still always false. – pookie Feb 01 '16 at 12:16
  • @pookie : Check also [this](http://stackoverflow.com/questions/5835848/when-will-ofstreamopen-fail) answer for further info about possible errors plus, of course, the official Linux page about [`open(2)`](http://linux.die.net/man/2/open). – Michael Feb 01 '16 at 12:17
  • @pookie: Running self-written, untested software in admin mode (where it could wreck *everything* in your system) is a **bad** idea. Don't **ever** do that. Well, what happens if you give an *absolute* path? What *is* the result of `GetCurrentDirectory()` at the point you're trying to open the file? You might want to try [fopen()](http://en.cppreference.com/w/cpp/io/c/fopen) followed by [`perror()`](http://en.cppreference.com/w/cpp/io/c/perror) instead to get an error message (although I am not sure if Windows *does* set `errno`, that's a POSIX requirement, not a standard one). – DevSolar Feb 01 '16 at 12:24
  • @DevSolar Thank you, I will try your suggestion. In the meanwhile, I would like to know why running self-written, untested software in administrator mode is a bad idea. Please answer here: http://stackoverflow.com/questions/35130600/why-is-running-self-written-untested-code-in-administrator-mode-a-bad-idea – pookie Feb 01 '16 at 12:32
  • @DevSolar adding an absolute path results in the same problem. `GetCurrentDirectory` returns `C:\Users\pookie\Documents\Visual Studio 2015\Projects\demoDX12\x64\Debug\demoDX12\AppX` – pookie Feb 01 '16 at 12:59
  • @pookie: Well, you're losing me there. I only use Windows for games or for logging into Unix machines. ;-) The fine print of Windows access rights eludes me. Try the `fopen()` / `perror()` approach, that might give you a proper description of the error. – DevSolar Feb 01 '16 at 13:07
  • @DevSolar No bother, thanks for taking the time to help. – pookie Feb 01 '16 at 13:08