Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2001-09-05
9
Medium Priority
?
367 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 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.
Suggested Courses

610 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