Using std:fstream how to deny access (read and write) to the file
You cannot do it with the standard fstream, you'll have to use platform specific functions.
On Windows, you can use CreateFile() or LockFileEx(). On Linux, there is flock(), lockf(), and fcntl() (as the previous commenter said).
If you are using MSVC, you can pass a third parameter to fstream's constructor. See the documentation for Visual Studio 6 or newer versions. Of course it won't work with other compilers and platforms.
Why do you want to lock others out anyway? There might be a better solution...
c++ fstream concurrent access
There is certainly no portable way to do efficient file sharing (with simultaneous access) using C++.
You can share files using a "lock" file. Before opening "foo.dat", try to create file "foo.lock". Keep looping until you succeed. After access, delete foo.lock. That allows serial access, but not concurrent access.
You can use byte-level locking in platform-specific ways. Windows has LockFileEx(). POSIX has fcntl and flock. If you need multi-platforms you will need separate implementations. You can encapsulate them in a class and use #if to handle the platform-specific bits.
This is the most efficient (fastest) by a lot, but it involves very complex programming and is prone to bugs.You can use a DBMS.
A DBMS will be simplest by a lot, but it does tie you to an external product which may or may not be a problem. Byte-wise locking is much faster than anything else, but will add a lot to devel and maintenance costs.
Protecting read/write access to/from a file from multiple threads
Here's the algorithm each thread should follow:
- Acquire the lock that protects the shared state of files.
- See if the file we are trying to access exists in the table.
- If it does not exist, create it.
- Check the entry for the file we are trying to access.
- If no other thread is accessing the file, jump to step 9.
- Release the lock that protects the shared state of files.
- Wait
- Go to step 1.
- Mark the file in use.
- Release the lock that protects the shared state of files.
- Read or write the file as appropriate.
- Acquire the lock that protects the shared state of files.
- Mark the file not in use.
- Release the lock that protects the shared state of files.
Note that if you use a condition variable to make the wait more efficient, then steps 6, 7 and 8 turn into waiting on the condition variable and then jumping to step 2. Also, you would need to broadcast the condition variable (notify all) before or after step 14. (Preferably before.)
c++ filestream problems when opening file in read write mode
Does it mean that I can't open a file in both read write mode at the same time?
No, you can do this, but the question is whether you can create a file by doing so.
Generally you'll need to add the trunc
flag (ironically one of the options for how to handle an existing file), or remove the in
flag (see here).
Yes, this is a bit of a pain, but it comes from how the original POSIX APIs work. Blame them!
Creating an empty file first and then running the above code, fs.fail() is false always. What is the rational for such a behavior by the fstream class?
You can always open a file that exists (well, subject to permissions). That behaviour makes sense.
std::ofstream does not show error when permission denied C++
I'd say your code works and the write operation is actually done, but for the sake of it, add a check after the write too:
outfile << err_line << endl;
if(outfile.fail()) cout << "Error3\n";
else cout<<"OK";
On my system, I'll get your Error 1 ... can't open file
if the file isn't opened for writing successfully.
Edit: Or are you running Windows with Compatibility Files
virtualization still active? If so, the file will probably be in the Virtual Store, not in the real C:\err.txt
path.
Example: C:\Users\username\AppData\Local\VirtualStore
If you find it there, you may find a lot of other stuff in there too. At least I did years ago when I had a similar problem. I decided to manually move (with admin rights) the few important files that some of my older programs had put there and then turn Virtual Store off. I can't find a good and simple official Microsoft link for how to turn off file and registry virtualization right now so perhaps this will do:
RegEdit:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\
Create a DWORD
Key with the name EnableVirtualization
and give it the value 0
. If the key is already there, but set to something else than zero, change it.
There's more here:
UAC Group Policy Settings and Registry Key Settings
How to use a single fstream for creation, reading and writing a file
Only with fstream I'd say no.
You might want to have something similar with the trunc mode but you'll lose everything if the file exists (which might be a problem, if not go for trunc + out)
The other way is to check if the file exists, if not you create it (whichever way). Then you open with In and Out and do your stuff.
It kind of doesn't make sense to be able to read inside an empty file you just created from the cpp point of view
Related Topics
Efficiently Getting All Divisors of a Given Number
How to Break When a Specific Exception Type Is Thrown in Gdb
Differencebetween #Define and Const
Training Custom Svm to Use with Hogdescriptor in Opencv
Structured Binding with [[Maybe_Unused]]
C++ Boost: What's the Cause of This Warning
How Can Std::Unique_Ptr Have No Size Overhead
Differencebetween Using a Struct with Two Fields and a Pair
Request for Member '...' Is Ambiguous in G++
Does C++ Contain the Entire C Language
Passing Variable Number of Arguments with Different Type - C++
Order of Calling Base Class Constructor from Derived Class Initialization List
Issue with Cin When Spaces Are Inputted, Using String Class
Get Sum of Values Stored in _M256D with Sse/Avx