CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Threaded View

  1. #7
    Join Date
    Nov 2006
    Location
    ntdll.dll
    Posts
    29

    Re: How to improve the File Merging

    Heres a slightly modified version of code I was using in an App i wrote awhile back.

    Code:
    // Open Handle to the two files
    HANDLE hFileOne = CreateFileA( "FileOne.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
    HANDLE hFileTwo = CreateFileA( "FileTwo.txt", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
    
    // Error check
    if ( hFileOne == INVALID_HANDLE_VALUE || hFileTwo == INVALID_HANDLE_VALUE )
    	return 0;
    
    // Get File size
    int iSizeOne = GetFileSize( hFileOne, NULL );
    
    // Read FileOne
    char* strBuffer = new char[ iSizeOne ];
    DWORD dwBytesRead = NULL;
    DWORD dwBytesWritten = NULL;
    
    if ( !ReadFile( hFileOne, strBuffer, iSizeOne, &dwBytesRead, NULL ) )
    	return 0;
    
    // Set position to end of file for appending.
    SetFilePointer( hFileTwo, 0, 0, FILE_END );
    
    // Write to FileTwo
    if ( !WriteFile( hFileTwo, strBuffer, dwBytesRead, &dwBytesWritten, NULL ) )
    	return 0;
    
    // Cleanup
    CloseHandle( hFileOne );
    CloseHandle( hFileTwo );
    
    delete strBuffer;
    I'd probably go with Philip Nicoletti's recommendation here tho
    Last edited by zeRoau; June 14th, 2009 at 01:11 PM. Reason: .
    Tom

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured