James Lent
James Lent

Reputation: 99

ofstream fails to open existing file

While working with the OpenXcom app, I noticed that at some point the app stopped updating the options.cfg file. I tracked this issue down to a failure creating an ofstream object for the options.cfg file in the Options::save() method. A search uncovered no explanation for my issue. The only solution I could find was to delete the options.cfg file and let the program recreate it with the default settings.

I have recreated what appears to be a similar issue with a very simple C++ program (modeled on the first few lines of the Options::save() method in the OpenXcom project):

#include <iostream>
#include <fstream>

void check(const std::string& filename)
{
    std::ofstream stream(filename.c_str());

    if (!stream) {
        std::cout << "FAIL " << filename << std::endl;
    }
    else {
        std::cout << "PASS " << filename << std::endl;
    }
}

int main()
{
    check("options1.cfg");
    check("options2.cfg");
}

Using this test program, I created the following test case using the Command Prompt:

Here is Command Prompt listing for the test case (which I have edited a bit to remove extraneous information):

C:\...\test>dir

12/19/2022  09:10 AM            27,648 ofstream-test.exe

C:\Users\jwlent\Documents\test>ofstream-test.exe
PASS options1.cfg
PASS options2.cfg

C:\...\test>dir

12/19/2022  09:10 AM            27,648 ofstream-test.exe
12/19/2022  02:39 PM                 0 options1.cfg
12/19/2022  02:39 PM                 0 options2.cfg

C:\...\test>ofstream-test.exe
PASS options1.cfg
PASS options2.cfg

C:\....\test>del options2.cfg

C:\....\test>copy options1.cfg options2.cfg
        1 file(s) copied.

C:\...\test>dir

12/19/2022  09:10 AM            27,648 ofstream-test.exe
12/19/2022  02:39 PM                 0 options1.cfg
12/19/2022  02:39 PM                 0 options2.cfg

C:\....\test>ofstream-test.exe
PASS options1.cfg
FAIL options2.cfg 

Other things I noted:

Can anyone explain what may be going on? Especially if there is a way to modify the test program to work around this issue in a way that can be applied to the Options::save() file in the OpenXcom project.

A few more observations:

Here is the slightly edited Command Prompt output.

C:\...\test>ofstream-test.exe
PASS options1.cfg
FAIL options2.cfg
    
C:\...\test>python
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
>>> f = open("options2.cfg", "w")
>>> f.write("overwrite")
>>> f.close()
>>> exit()
    
C:\...\test>ofstream-test.exe
PASS options1.cfg
FAIL options2.cfg
    
C:\...\test>more options2.cfg
 overwrite

Here is what Process Monitor recorded for the C++ program and then Python trying to open the file for write:

46:23.0 ofstream-test.exe 22396 CreateFile C:\Users\jwlent\Documents\test\options2.cfg ACCESS DENIED Desired Access: Generic Write, Read Attributes, Disposition: OverwriteIf, Options: Synchronous IO Non-Alert, Non-Directory File, Attributes: N, ShareMode: Read, Write, AllocationSize: 0

46:39.9 python.exe 3000 CreateFile C:\Users\jwlent\Documents\test\options2.cfg SUCCESS Desired Access: Generic Write, Read Attributes, Disposition: OverwriteIf, Options: Synchronous IO Non-Alert, Non-Directory File, Attributes: N, ShareMode: Read, Write, AllocationSize: 0, OpenResult: Overwritten

Upvotes: 4

Views: 146

Answers (0)

Related Questions