Using Std:Fstream How to Deny Access (Read and Write) to the File

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++.

  1. 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.

  2. 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.

  3. 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:

  1. Acquire the lock that protects the shared state of files.
  2. See if the file we are trying to access exists in the table.
  3. If it does not exist, create it.
  4. Check the entry for the file we are trying to access.
  5. If no other thread is accessing the file, jump to step 9.
  6. Release the lock that protects the shared state of files.
  7. Wait
  8. Go to step 1.
  9. Mark the file in use.
  10. Release the lock that protects the shared state of files.
  11. Read or write the file as appropriate.
  12. Acquire the lock that protects the shared state of files.
  13. Mark the file not in use.
  14. 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



Leave a reply



Submit