Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Appending One File to Another File

Total Hit ( 3934)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


The following example demonstrates opening and closing files, reading and writing files, and locking and unlocking files. The application appends one file to the end of another file. The application opens the file having the new data appended with permissions that allow only this application to write to it, but other processes may open the file for reading during the append process. This allows other processes to see partial results of the append operation while it is in progress. In order for the readers to get a consistent view of the file, it is locked during the write operation.

The application opens two files by using CreateFile: One.txt is opened for reading, and Two.txt is opened for writing and shared reading. Then the application appends the contents of One.txt to the end of Two.txt by reading and writing 4K blocks by using ReadFile and WriteFile. Before writing to the second file, the application sets the second file's pointer to the end of the file by using SetFilePointer and locks the area to be written by using LockFile. This prevents another thread or a process with a duplicated handle from accessing the area while the write is in progress. After each write operation, UnlockFile unlocks the locked area. It is not necessary for the call to SetFilePointer to occur after the file is locked because only one process can have the file open for write.

Step-By-Step Example

- Create a standard exe project
- Add the following code in form1
- Before you run the demo change fPath1 and fPath2 if you dont have one.txt and two.txt in the same folder of project files

Click here to copy the following block
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const OPEN_EXISTING = 3
Private Const OPEN_ALWAYS As Long = 4
Private Const CREATE_ALWAYS = 2
Private Const INVALID_HANDLE_VALUE = -1&

Private Const FILE_END As Long = 2
Private Const FILE_BEGIN As Long = 0
Private Const FILE_CURRENT As Long = 1

Private Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" ( _
    ByVal lpFileName As String, _
    ByVal dwDesiredAccess As Long, _
    ByVal dwShareMode As Long, _
    ByRef lpSecurityAttributes As Any, _
    ByVal dwCreationDisposition As Long, _
    ByVal dwFlagsAndAttributes As Long, _
    ByVal hTemplateFile As Long) As Long

Private Declare Function ReadFile Lib "kernel32.dll" ( _
    ByVal hFile As Long, _
    ByRef lpBuffer As Any, _
    ByVal nNumberOfBytesToRead As Long, _
    ByRef lpNumberOfBytesRead As Long, _
    ByRef lpOverlapped As Any) As Long

Private Declare Function SetFilePointer Lib "kernel32.dll" ( _
    ByVal hFile As Long, _
    ByVal lDistanceToMove As Long, _
    ByRef lpDistanceToMoveHigh As Long, _
    ByVal dwMoveMethod As Long) As Long

Private Declare Function LockFile Lib "kernel32.dll" ( _
    ByVal hFile As Long, _
    ByVal dwFileOffsetLow As Long, _
    ByVal dwFileOffsetHigh As Long, _
    ByVal nNumberOfBytesToLockLow As Long, _
    ByVal nNumberOfBytesToLockHigh As Long) As Long

Private Declare Function WriteFile Lib "kernel32.dll" ( _
    ByVal hFile As Long, _
    ByRef lpBuffer As Any, _
    ByVal nNumberOfBytesToWrite As Long, _
    ByRef lpNumberOfBytesWritten As Long, _
    ByRef lpOverlapped As Any) As Long

Private Declare Function UnlockFile Lib "kernel32.dll" ( _
    ByVal hFile As Long, _
    ByVal dwFileOffsetLow As Long, _
    ByVal dwFileOffsetHigh As Long, _
    ByVal nNumberOfBytesToUnlockLow As Long, _
    ByVal nNumberOfBytesToUnlockHigh As Long) As Long

Private Declare Function CloseHandle Lib "kernel32.dll" ( _
    ByVal hObject As Long) As Long

Const MAX_BUFF = 4096
Dim hFile As Long
Dim hAppend As Long
Dim dwBytesRead As Long, dwBytesWritten As Long, dwPos As Long
Dim buff(MAX_BUFF) As Byte
Dim fPath1 As String, fPath2 As String

Private Sub Form_Load()
  '// Open the existing file.

  fPath1 = App.Path & "\one.txt"
  fPath2 = App.Path & "\two.txt"

  hFile = CreateFile(fPath1, _
      GENERIC_READ, _
      0, _
      ByVal 0&, _
      OPEN_EXISTING, _
      FILE_ATTRIBUTE_NORMAL, _
      ByVal 0&)

  If (hFile = INVALID_HANDLE_VALUE) Then

    MsgBox "Could not open One.txt."
    Exit Sub
  End If

  '// Open the existing file, or if the file does not exist,
  '// create a new file.

  hAppend = CreateFile(fPath2, _
      GENERIC_WRITE, _
      FILE_SHARE_READ, _
      ByVal 0&, _
      OPEN_ALWAYS, _
      FILE_ATTRIBUTE_NORMAL, _
      ByVal 0&)

  If (hAppend = INVALID_HANDLE_VALUE) Then
    MsgBox "Could not open Two.txt."
    Exit Sub
  End If

  '// 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 finished.
  '
  Dim totalBytesAppended As Long

  Do
    If (ReadFile(hFile, buff(0), MAX_BUFF + 1, dwBytesRead, ByVal 0&)) Then
      dwPos = SetFilePointer(hAppend, 0, ByVal 0&, FILE_END)

      '//Lock the memory area at the end of the file so nobody else can write there
      Call LockFile(hAppend, dwPos, 0, dwBytesRead, 0)
      Call WriteFile(hAppend, buff(0), dwBytesRead, dwBytesWritten, ByVal 0&)

      totalBytesAppended = totalBytesAppended + dwBytesWritten
      '//Now unlock the memory region
      Call UnlockFile(hAppend, dwPos, 0, dwBytesRead, 0)
    End If
  Loop While (dwBytesRead = MAX_BUFF + 1)

  '// Close both files.

  CloseHandle (hFile)
  CloseHandle (hAppend)

  MsgBox totalBytesAppended & " Bytes appened to the file"
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.