1

I have a python program, which loads a python module created with c++ and imported as dll. In the python program, I created a file called cpp.conf in the working directory.

with open('cpp.conf', 'w') as cppconf:
    cppconf.write("blabla")

The c++ module is imported later by the python program. On the c++ side, I tried to open the file cpp.conf, but it failed.

std::string configFile = "cpp.conf";
std::ifstream ifs(configFile.c_str());
if (ifs.good()) {
    std::cout << "opened file successfully." << std::endl;
}
else {
    std::cout << "failed to open file." << std::endl;
}

However, after the program is finished, I can see the file cpp.conf. I thought it could be that the c++ needs some time to let the file be seen on the disk, so I tried the following:

with open('cpp.conf', 'w') as cppconf:
    cppconf.write("blabla")

import time
time.sleep(5) # wait 5 seconds

it did not help either.

If the python program did not touch the file, that is, did not open it to write, the c++ side can open the file successfully.

Is there a reason to this effect?

The platform is windows 7, I did not try on Linux.

SOLVED!

1
  • problem solved. not really that mysterious, I made a mistake in the python program and the c++ program could not parse it correctly. Commented Apr 30, 2016 at 8:26

1 Answer 1

2

May be you're running the python program with administrative privileges and the C++ program with another user that is not allowed to read it?

Note also that the file location may be important. On windows, for reasons not entirely clear to me, the system sometimes just tricks processes into being able to create a file in a certain directory (e.g. C:\)while instead they're created in another "safer" place.

This can be a nightmare to debug because the OS is just lying to the programs...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I solved this problem. The reason was that I made a mistake in the text file created by python, the file has been opened by c++, but was not parsed correctly. I should have debugged more before I asked this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.