Solved

Should be easy!... Locking access to a file

Posted on 2001-09-05
9
319 Views
Last Modified: 2007-11-27
Hi,

I'm looking for a very basic, easy way to lock a file which is getting overwritten by another application while I am working with it - at least I think that is the problem.

Either way, I open the file using:

   ifstream TestFile;
   TestFile.open("file.txt");
.
.
.
   TestFile.close();


And I'm looking for a switch I can throw in there which would put a lock on the file so nothing can change it while I am accessing it.

Am I having a severe brain cramp here? This seems like a very common situation.

None of the open modes seem to apply. I don't think this should be a tough problem, but I can't find anything that doesn't seem to require a huge amount of code to accomplish something that seems pretty standard.

What am I missing? Any suggestions?

Thanks
0
Comment
Question by:joemacd
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6458785
C++ does not handle this, as it's OS-specific - '_locking()' should be what you're looking for:

/* LOCKING.C: This program opens a file with sharing. It locks
 * some bytes before reading them, then unlocks them. Note that the
 * program works correctly only if the file exists.
 */

#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/locking.h>
#include <share.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

void main( void )
{
   int  fh, numread;
   char buffer[40];

   /* Quit if can't open file or system doesn't
    * support sharing.
    */
   fh = _sopen( "locking.c", _O_RDWR, _SH_DENYNO,
                 _S_IREAD | _S_IWRITE );
   if( fh == -1 )
      exit( 1 );

   /* Lock some bytes and read them. Then unlock. */
   if( _locking( fh, LK_NBLCK, 30L ) != -1 )
   {
      printf( "No one can change these bytes while I'm reading them\n" );
      numread = _read( fh, buffer, 30 );
      printf( "%d bytes read: %.30s\n", numread, buffer );
      lseek( fh, 0L, SEEK_SET );
     _locking( fh, LK_UNLCK, 30L );
      printf( "Now I'm done. Do what you will with them\n" );
   }
   else
      perror( "Locking failed\n" );

   _close( fh );
}
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6459007
just use

ifstream TestFile;

TestFile.Open("File.txt",ios::in, filebuf::sh_none || filebuf::sh_read || filebuf::sh_write)

Vin.
0
 
LVL 3

Expert Comment

by:ruff_ryder
ID: 6459118
Yep as the guys above said, read up on the iostream flags that can be used to set permissions on files
0
 
LVL 4

Accepted Solution

by:
VincentLawlor earned 100 total points
ID: 6459912
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:joemacd
ID: 6460858
VincentLawlor,

That looks like the ticket, but is this standard stuff?

I'm using C++ Builder, and for some reason I'm getting an error on the flag.

Specifically, I'm getting:

Undefined symbol 'sh_none'
0
 

Author Comment

by:joemacd
ID: 6460916
VincentLawlor,

That looks like the ticket, but is this standard stuff?

I'm using C++ Builder, and for some reason I'm getting an error on the flag.

Specifically, I'm getting:

Undefined symbol 'sh_none'
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6460979
Hmm

filebuf::sh_none

This should be defined in fstream.h

Vin.

0
 
LVL 11

Expert Comment

by:griessh
ID: 6865813
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "jkr" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 

Author Comment

by:joemacd
ID: 6867903
Didn't really work for me in C++ Builder, but it should have.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now