0
votes

I am attempting to open the current process's executable file for read-write operations (I have additional data attached to the executable), however std::fstream will not open the file in ios::in | ios::out | ios::binary mode, even though open() will (with O_RDWR flag set).

Does anyone know why std::fstream will not open the file, while open() will, and if so, how I can get std::fstream to open the file?

2

2 Answers

1
votes

It most probably has to do with file sharing semantics. See this thread which deals with a similar question - and the answer there is "The concept of file protection, file sharing, file permissions is OS-specific, which is why it is not covered by standard C++".

0
votes

I'd wonder if, somewhere, under the hood, the std::fstream version is calling open - I would bet so. Have you tried checking errno anyways, or perhaps running strace to see what, if any errors, the system call is returning?