Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to lock a file when it is in use?

How to lock a file when it is in use?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tagopi
    wrote on last edited by
    #1

    Hello everybody, i am having text file opened in some other application(our application). if i delete this opened file from windows explorer, its deleting, so, at some point, my application crashes (while trying to write). how can i lock that file when it is use in other application? i am using VC++ with win32. Thanks in advance, A. Gopinath.

    M S 2 Replies Last reply
    0
    • T tagopi

      Hello everybody, i am having text file opened in some other application(our application). if i delete this opened file from windows explorer, its deleting, so, at some point, my application crashes (while trying to write). how can i lock that file when it is use in other application? i am using VC++ with win32. Thanks in advance, A. Gopinath.

      M Offline
      M Offline
      Malli_S
      wrote on last edited by
      #2

      You can use CreateFile to open an existing file with without Sharing. i.e. passing dwShareMode of CreateFile()[^] as zero. Or you can open a file using OpenFile()[^] by passing 'OF_SHARE_DENY_READ |OF_SHARE_DENY_WRITE' to deny sharing.

      [Delegates]      [Virtual Desktop]      [Tray Me !]
      -Malli...! :rose:****

      1 Reply Last reply
      0
      • T tagopi

        Hello everybody, i am having text file opened in some other application(our application). if i delete this opened file from windows explorer, its deleting, so, at some point, my application crashes (while trying to write). how can i lock that file when it is use in other application? i am using VC++ with win32. Thanks in advance, A. Gopinath.

        S Offline
        S Offline
        Software_Developer
        wrote on last edited by
        #3

        [LockFile] locks the second file to prevent another process from accessing it while writing to it. Sample code:

        #include
        #include

        void main()
        {
        HANDLE hFile;
        HANDLE hAppend;
        DWORD dwBytesRead, dwBytesWritten, dwPos;
        BYTE buff[4096];
        char DataBuffer[] = "This is some test data to write to the file.";
        DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);

        BOOL bErrorFlag = FALSE;
        

        // Open the existing file.

        hFile = CreateFile(TEXT("one.txt"), // open One.txt
        GENERIC_READ, // open for reading
        0, // do not share
        NULL, // no security
        CREATE_ALWAYS, // existing file only
        FILE_ATTRIBUTE_NORMAL, // normal file
        NULL); // no attr. template

        if (hFile == INVALID_HANDLE_VALUE)
        {

         printf("Could not open One.txt."); 
         return;
        

        }
        bErrorFlag = WriteFile(
        hFile, // open file handle
        DataBuffer, // start of data to write
        dwBytesToWrite, // number of bytes to write
        &dwBytesWritten, // number of bytes that were written
        NULL); // no overlapped structure

        // Open the existing file, or if the file does not exist,
        // create a new file.

        hAppend = CreateFile(TEXT("two.txt"), // open Two.txt
        FILE_APPEND_DATA, // open for writing
        FILE_SHARE_READ, // allow multiple readers
        NULL, // no security
        CREATE_ALWAYS, // open or create
        FILE_ATTRIBUTE_NORMAL, // normal file
        NULL); // no attr. template

        if (hAppend == INVALID_HANDLE_VALUE)
        {
        printf("Could not open Two.txt.");
        return;
        }

        // Append the first file to the end of the second file.
        // Lock the second file to prevent another process from
        // accessing it while writing to it. Unlock the
        // file when writing is complete.

        while (ReadFile(hFile, buff, sizeof(buff), &dwBytesRead, NULL)
        && dwBytesRead > 0)
        {
        dwPos = SetFilePointer(hAppend, 0, NULL, FILE_END);
        LockFile(hAppend, dwPos, 0, dwBytesRead, 0);
        WriteFile(hAppend, buff, dwBytesRead, &dwBytesWritten, NUL

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups