Solved

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

Posted on 2001-09-05
9
344 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 4

Accepted Solution

by:
VincentLawlor earned 100 total points
ID: 6459912
0
 

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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 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