zoukankan      html  css  js  c++  java
  • 文件锁简单操作(lockfileExunlockfileEx)

    #include "stdafx.h"
    #include <Windows.h>
    #include <iostream>

    using namespace std;


    int _tmain (int argc, LPTSTR argv[])
    {
        

        OVERLAPPED Oapped;
        FILE*fp=NULL;
        HANDLE hDir = CreateFile( argv[1],                   // lpFileName
                               GENERIC_WRITE | GENERIC_READ,               // dwDesiredAccess
                               0,                          // dwShareMode
                               NULL,                       // lpSecurityAttributes
                               CREATE_ALWAYS,              // dwCreationDisposition
                               FILE_ATTRIBUTE_NORMAL, // dwFlagsAndAttributes
                               NULL );

      

        int i=LockFileEx(hDir, LOCKFILE_FAIL_IMMEDIATELY, (DWORD)0, (DWORD)200,  (DWORD)0, &Oapped);

    if (i!=0)
      {
            cout<<"file lock success!!"<<endl;
            Sleep(10000);
      }


    int n =UnlockFileEx(hDir, (DWORD)0, (DWORD)200,(DWORD)0, &Oapped);

     CloseHandle(hDir);

    if (n!=0)
      {cout<<"file unlock success!!"<<endl;
     
             Sleep(1000);

      if((fp=fopen("e:\test.txt","w"))==NULL)
              {
                    cout<<"Can't open !"<<endl;
              }
      else
            {
            cout<<"open success!";
            }
    }

        system("pause");
        return 0;
    }

  • 相关阅读:
    Leetcode Word Pattern
    Leetcode Strobogrammatic Number
    Leetcode Meeting Rooms
    Leetcode Pascal's Triangle II
    Leetcode Pascal's Triangle
    Leetcode Majority Element II
    Leetcode Majority Element
    牛客多校第六场 B Shorten IPv6 Address 模拟
    牛客多校第六场 A Garbage 模拟/签到
    排列数,康托展开及其线段树优化
  • 原文地址:https://www.cnblogs.com/duyy/p/3631100.html
Copyright © 2011-2022 走看看