473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

File locking on windows

180 100+
Hi guys,,

What is wrong with below given code. It isnt locking the file???Where m I going wrong???
Expand|Select|Wrap|Line Numbers
  1. int main (){
  2.  
  3.    FILE    *pFile = NULL;  
  4.    char    *fileName = "two.txt";
  5.  
  6.    // Open the file in read mode  
  7.    pFile = fopen( fileName ,"r");   
  8.  
  9.    if (!pFile){
  10.          printf("Error opening file %s!\n", fileName);
  11.          exit(1);
  12.    }  
  13.    int beg = 0;
  14.    fseek( pFile, beg, SEEK_END  );
  15.    int end = ftell(pFile);  
  16.  
  17.    cout<<"Press <RETURN> to try to get lock: "<<endl;
  18.    getchar();
  19.    cout<<"Trying to get lock..."<<endl;
  20.  
  21.    if( !LockFile((HANDLE)pFile, end, 0, end, 0) )
  22.    {
  23.        cout<< "Cannot lock:"<<endl;
  24.        return 0;
  25.    }
  26.  
  27.    fseek( pFile, beg, SEEK_SET  );     
  28.    ifstream is(pFile);
  29.    char line[256];
  30.    while( !is.eof() ){  
  31.            is.getline( line, sizeof( line ) );  
  32.            cout<<line;  
  33.    }
  34.    cout<<"Press <RETURN> to release lock: "<<endl;
  35.    getchar();
  36.    UnlockFile((HANDLE)pFile, 0, 0, end, 0);
  37.    cout<<"Unlocked.\n"<<endl;    
  38.    fclose( pFile );
  39.  
  40.    return 0;  
  41.  }
Hi Banfa, can you please help me with this. I want to lock a file on windows (Microsoft Visual Studio .NET 2003) such that other processes are unable to read the same file again. Please help...
May 24 '07 #1
1 1976
weaknessforcats
9,208 Expert Mod 8TB
I don't believe LockFile prevents reading from a mapped file view.

You may need to build a semaphore class to prevent reading.
May 24 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Alex | last post by:
Is there any way to link a table to a front end for reference only (no requirement to edit data) so that the LDB file is not created. I use a number of tables for reference and do not need to edit...
0
by: Andrew Dowding | last post by:
Hi Everybody, I have been looking at problems with my Windows Forms C# application and it's little Jet 4 (Access) database for the last few days. The Windows Forms app implements a facade and...
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
3
by: dchadha | last post by:
Hi, I am working on application in C# which uses and stores data in xml file. This app can use xml file from two different ways: (a) From User Interface (Windows application) and (b) From...
17
by: shineofleo | last post by:
Here is the situation: I wrote a VB programm, which stores all the information in a single Access database file using jet engine. It worked well, however one of my customs reported that there was...
17
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: ...
5
by: JM | last post by:
I have created a Windows Service which uses xml file as a data source. The service is running on .NET 2.0 and uses LocalSystem account. It was running fine but now it has started locking the xml...
26
by: Don Calloway | last post by:
I have created a <filename>.db1 (where <filenameis the name given to the database) Access database that is being used in multi-user mode. When the <filename>.db1 file is opened by a user, the...
7
by: =?Utf-8?B?UGFycm90?= | last post by:
I am receiving a "file is being used by another process" error message whenever I try to access a text file that I had previously created and then closed. This only happens when I test in a...
2
by: Sparky | last post by:
Hello! I am writing some software that will have many users accessing the same file resource at once for reading purposes only. I am programming on (Ubuntu) Linux and my question is in Windows, can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.