I want to open a file for read and write with std::filebuf, but create the file if it doesn't exist yet. If it already exists, do nothing. I want to freely write into the file, without any "always jump to end before each write" behavior. What open mode should be used?
According to the table , there appears to be no combination of flags to do that.
What's the best option to ensure this effect?
Related
C++ In Windows 7.
When writing to my log file, i sometimes set a breakpoint, or the program gets stuck at something. When i try too peek in my logfile from another program it says "The file cannot be opened because it is in use by another process". Well thats true, however I've worked with other programs that still allows reading from a logfile while they are writing to it, so I know it should be possible. Tried the _fsopen and unlocking the file but without success.
FILE* logFile;
//fopen_s(&logFile, "log.log", "w");
logFile = _fsopen("log.log", "w", _SH_DENYNO);
if (!logFile)
throw "fopen";
_unlock_file(logFile);
If you have the log-file open with full sharing-mode, others are still stopped from opening for exclusive access, or with deny-write.
Seems the second program wants more access than would be compatible.
Also, I guess you only want to append to the log, use mode "a" instead of "w".
Last, do not call _unlock_file unless you called _lock_file on the same file previously.
There is a way to do what you want though:
Open your file without any access, and then use Opportunistic Locks.
Raymond Chen's blog The Old New Thing also has a nice example: https://devblogs.microsoft.com/oldnewthing/20130415-00/?p=4663
I try to write to an existing file with QFile, which works as expected. However, the problem is that if the file is open in Excel, writing to the file from my program fails.
I try to test the permissions with QFileInfo and have all read and write permissions on the file. The test
bool opened = file-> open (QIODevice :: WriteOnly)
returns true.
The same problem does not occur when the file is opened with notepad++.
How can I check if the file is locked and can't be written to?
Excel locks its open files for exclusive use. You can't write to an open file, move or delete it. There is no way to bypass this lock.
See also: Write to locked file regardless of lock status
When you use the QFile::write function, it returns the number of bytes written, or -1 if an error occurred.
If you check the return code from the write function, you should be able to use that to determine that the file is locked by another process.
Calling QFile::open returns without error, because you can still get a valid handle to the file, even though another process has locked it, preventing you writing to it at the same time.
As the title states:
My program opens a file.
Something comes along and moves that file. Inode should be the same, but name is different.
Close the file, then delete, but its not there anymore
So how can I detect that it has been moved and delete the correct filename?
Any ideas?
You could use inotify to detect a change to the old name (look for the IN_MOVE_SELF event). But if your real goal is simply to delete the file's name, you can just do that (using unlink(2)) immediately after opening the file. Unix file semantics will allow you to keep using the open file, and the data on disk will not actually be deleted until your handle is closed. And then no one will be able to rename the file.
try to access(2) the file before closing it. If you get ENOENT, the file has been moved. Follow the /proc/self/fd/$open_file_number/ symlink to find the new filename using readlink(2).
fstat the open file, stat the name, and compare the results.
But, in common with any any possible check, it is still racy; the name may change meaning after any check you make but before you act upon it. Time to revisit your requirements.
I'm wondering whether making files read-only so the user can't mess with them will disallow my program from writing information to them via an fstream.
Yes. If a file is read-only, it's read-only. Why not unset the read-only bit, write to the file, and reset it? The lock that you get on the file while writing to it should prevent users from making modifications to it while your application is writing to it. However, IMHO, the whole exercise is pointless, since it takes exactly 4 clicks to make a file writable, so your users can change the file whenever they want anyway. What I'd do is make an md5 or sha1 hash of the file, store it in the registry and check to see if that's changed on application startup.
If you open a file read-only, you can't write to it.
If you are looking to open a file that you can write to but nobody else can, then (in Windows) you are looking for file sharing attributes.
Under Windows is there a way to modify a file/executable opened by another process using c++?
Is there a way to modify an open executable in windows?
No.
Is there a way to modify an open file in windows using c++?
Yes. If it has been opened with the proper share permissions. See http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx FILE_SHARE_WRITE
It may be possible but perhaps not easy to achieve. You need inject thread in destination process and know PE format for correctly edit opened file and modify it.
All information is on web.
Good Luck.
I find this freeware tool, it proposes to unlock files and folders.
The OS holds the executable file open for read-only sharing as long as it's running, so there's no way to modify it directly. You can, however, open it for reading (if you specify read-sharing in your CreateFile call), and make a modified copy of it, while it's running.
I don't know if that's what you had in mind, but if it's your own program you're doing this to, you can start the new copy and have it pick up where the previous one left off... not straightforward, but not all that difficult either.