[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