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;
    }

  • 相关阅读:
    【逆序对】N*M Puzzle / Simple Puzzle
    【逆序对】Ultra
    bzoj 1814 Fornula 1
    hdu 1693 插头dp入门
    bzoj 2154
    没有上司的舞会
    【HNOI】合唱队
    luogu 自适应Simpson2
    luogu 自适应Simpson1
    【bzoj 1502】月下柠檬树
  • 原文地址:https://www.cnblogs.com/duyy/p/3631100.html
Copyright © 2011-2022 走看看