October 10, 2012

[ Win32 MFC ] - File Processing using CreateFile, ReadFile and WriteFile APIs

1) Introduction

In this article, we will look at the example of using the CreateFile and OpenFile win32 API functions with a MFC Dialog based application. Win32 is a very rich API that supplies numerous functions and MFC is just a Framework wrapped over those functions to form logical functioning unit. Win32 API library is in native format that means it is in C style (Procedural Approach) whereas MFC is a OOPS based Framework API. OK, let us start with the sample.

2) About the Sample

Have a look at the below screenshot

This sample is created to copy the file content from source location to destination location. This plain file content copy operation is already supported by the operating system and this example is taken just for demo purpose. However, you can extend the operation to transform source content to destination by skipping some words or adding something to the word etc.

Specify the file name to copy in the Source File Path and specify the destination file name in which you need a copy of the source file content. The CopyFile Win32 API will do this task easily. However, in this article we are going to explore the Win32 file processing functions. The sample is created using the VC++ dialog based application.

Creating the dialog based application is shown in the below video.

Once the dialog-based application is created, control variables added to the edit box controls. This is shown in the below video:

3) Copy Content Button Handler

1) First, the win32 handles to the files are declared and these handles are hcopysource, hCopyDest. Next the variables bytes_read, bytes_written are used to store number bytes read and written depending on the file processing operation. The buffer variable is used as the cache by the program to temporarily store the data read from the file.

//Sample 01: Declarations Required
HANDLE hcopysource, hCopyDest;
DWORD bytes_read, bytes_written;
CHAR buffer [4095];

2) Next, we read input entered by the user from the text box control variables to the string variables Source_file, Dest_file. The GetWindowText functions return the text key-in in the text boxes.

//Sample 02: Get the User input
CString Source_file, Dest_file;

3) The Win32 API function CreateFile is used to open the file entered by the user. The OPEN_EXISTING tag will tell the API to open the file when it already exits and fail otherwise. Once the file content that we are going to copy is opened, we store its handle in the hcopysource. The GENERIC_READ flag tells that we going to open the file for reading purpose.

//Sample 03: Create the Source File
if (hcopysource == INVALID_HANDLE_VALUE)
         AfxMessageBox("Unable to Open Source File");

4) The same way we store the destination file handle. Here it is expected that the file does not exist in the destination folder and we always try to create the file as new file in the location specified. The flag GENERIC_WRITE tells that we are going to use this file for writing something on it. The CREATE_ALWAYS attribute tells that we are always going to create the file. If it doesn’t exists in the destination location the API will create a new file and if it is there in that location then the function will just open it. Therefore, the tag always creates the file and gives back the handle.

//Sample 04: Create Destination File
if (hcopysource == INVALID_HANDLE_VALUE)
         AfxMessageBox("Unable to Destination Source File");

5) We use ReadFile API to read the data from the source file. Once the call succeeded, we will get the read content in the buffer variable. Note the usage of the while loop and when the file content is more that 4095 bytes, the read operation will be performed in batches. That is, 4095 or less (That will be last read) bytes will be read in each batch. The bytes_read variable will tell us how many bytes are read from the source file. Say for example, the file has 5000 bytes of data, and the first read batch will read all 4095 bytes the remaining 5 bytes are read in the next iteration. This way, we use the bytes_read variable when writing the data to the destination file using the API function WriteFile.

//Sample 05: Read the Source file content in a batch of 4095 or Lesser, then
//                        write to destination file
while (::ReadFile(hcopysource, buffer, 4095, &bytes_read, NULL) && bytes_read > 0)
         ::WriteFile(hCopyDest, buffer, bytes_read, &bytes_written, NULL);
         if (bytes_read != bytes_written)
                 AfxMessageBox("Something Gone wrong while writing to destination. Aborting the operation");
AfxMessageBox("File contents copied to destination");

6) Once the Operation is completed, we close the file HANDLES opened the button click event and display a message stating the content of the file copied to the destination.
//Sample 06: Do the Clean-Up work
Below given video shows the Copy file operation using the Win32 API used in Dialog based MFC Application.

Source Code : Download

No comments:

Post a Comment

Leave your comment(s) here.

Like this site? Tell it to your Friend :)