Solved

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

Posted on 2001-09-05
9
329 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

24 Experts available now in Live!

Get 1:1 Help Now