?
Solved

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

Posted on 2001-09-05
9
Medium Priority
?
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Accepted Solution

by:
VincentLawlor earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

765 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