Share via


_lock_file

Bloqueia um arquivo, impedindo que outros processos ao acessar o arquivo.

void _lock_file(
   FILE* file
);

Parâmetros

  • file
    Identificador de arquivo.

Comentários

The _lock_file função bloqueia o arquivo especificado por file. Um arquivo de bloqueio impede o acesso ao arquivo por outros processos.Use _unlock_file Para liberar o bloquear no arquivo. Chamadas para _lock_file e _unlock_file devem ser iguais em um thread. Tentativa de bloquear um arquivo já está bloqueado ou desbloquear um arquivo que não pode resultar em um deadlock.

Requisitos

Rotina

Cabeçalho necessário

_lock_file

<stdio.h>

Para obter mais informações de compatibilidade, consulte Compatibilidade na introdução.

Exemplo

// crt_lock_file.c
// This program will Lock a file,
// preventing other processes or threads
// from gaining access to the file until 
// it's unlocked.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE    *pFile = NULL;
    char    *fileName = "crt_lock_file.txt";
    char    commandLine[256];

    // Open the file in write mode
    fopen_s(&pFile, fileName ,"w+");
    if (!pFile)
    {
        printf("Error opening file %s!\n", fileName);
        exit(1);
    }

    // Lock the file.
    _lock_file(pFile);
    printf("Locking the file %s.\n", fileName);

    // Add some data to the file
    fprintf(pFile, "The sound of bagpipes in the distant glen...\n");

    printf("\nAttempting to access the file from another process:\n");
    sprintf_s(commandLine, sizeof(commandLine), "type %s", fileName);
    system(commandLine);

    // Unlock the file
    _unlock_file(pFile);

    // Close the file
    fclose(pFile);

    printf("\nAttempting to access the file again:\n");
    system(commandLine);
}

Locking the file crt_lock_file.txt.  Attempting to access the file from another process: The process cannot access the file because it is being used by another process.  Attempting to access the file again: The sound of bagpipes in the distant glen...

Equivalente do NET Framework

sistema::IO::FileStream::bloquear

Consulte também

Referência

Manipulação de arquivos

_creat, _wcreat

_Open, _wopen

_unlock_file