Click here to Skip to main content
       

C / C++ / MFC

 
GeneralRe: using static library for C code in Visual studio 2005 Pinmembersunny_vc12-Sep-12 3:42 
AnswerRe: using static library for C code in Visual studio 2005 Pinmemberjschell12-Sep-12 9:04 
GeneralRe: using static library for C code in Visual studio 2005 PinmemberMalli_S12-Sep-12 21:01 
GeneralRe: using static library for C code in Visual studio 2005 Pinmembersunny_vc13-Sep-12 0:55 
GeneralRe: using static library for C code in Visual studio 2005 Pinmemberjschell14-Sep-12 9:38 
QuestionHow to lock a file when it is in use? Pinmembertagopi12-Sep-12 1:46 
AnswerRe: How to lock a file when it is in use? PinmemberMalli_S12-Sep-12 2:56 
AnswerRe: How to lock a file when it is in use? PinmemberClark Kent SuperCoder13-Sep-12 6:43 
[LockFile] locks the second file to prevent another process from accessing it while writing to it.
 

Sample code:
 
#include <windows.h>
#include <stdio.h>
 
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, NULL);
    UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0);
    }
 
  // Close both files.

  CloseHandle(hFile);
  CloseHandle(hAppend);
}

QuestionGetting List of Specific Installed Programs PinmemberAmbiguousName11-Sep-12 7:02 
AnswerRe: Getting List of Specific Installed Programs PinmemberMichael_Lu11-Sep-12 9:21 
QuestionCan't select HBRUSH into device context PinmemberAlan Balkany11-Sep-12 6:38 
AnswerRe: Can't select HBRUSH into device context PinmemberJochen Arndt11-Sep-12 7:04 
GeneralRe: Can't select HBRUSH into device context PinmemberAlan Balkany11-Sep-12 7:10 
GeneralRe: Can't select HBRUSH into device context PinmemberJochen Arndt11-Sep-12 8:07 
GeneralRe: Can't select HBRUSH into device context PinmemberAlan Balkany11-Sep-12 8:08 
AnswerRe: Can't select HBRUSH into device context Pinmemberenhzflep11-Sep-12 11:20 
GeneralRe: Can't select HBRUSH into device context PinmemberAlan Balkany11-Sep-12 11:23 
GeneralRe: Can't select HBRUSH into device context Pinmemberenhzflep11-Sep-12 11:30 
GeneralRe: Can't select HBRUSH into device context PinmemberAlan Balkany13-Sep-12 11:47 
QuestionFile download help Pinmembersunilkpv10-Sep-12 2:04 
QuestionRe: File download help PinmemberDavidCrow10-Sep-12 3:38 
AnswerRe: File download help Pinmembersunilkpv10-Sep-12 20:04 
GeneralRe: File download help PinmemberFlaviu210-Sep-12 22:01 
QuestionRe: File download help PinmemberDavidCrow11-Sep-12 4:06 
QuestionHow to set Image' Document Size ? Pinmemberyogeshs9-Sep-12 23:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 24 Dec 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid