The Windows Disk Management 11

 

 

 

 

 

Example: Open a File for Reading

 

The following example uses CreateFile() to open an existing file for reading and ReadFile() to read up to 80 characters synchronously from the file. In this case, CreateFile() succeeds only if the specified file already exists in the current directory. A subsequent call to open this file with CreateFile() will succeed if the call uses the same access and sharing modes. You can use the file you created with the previous WriteFile() example to test this example.

Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.

 

 

Open a File for Reading program example: Creating new Win32 C++ console application project in Visual C++ .NET

 

Then, add the source file and give it a suitable name.

Next, add the following source code.

 

#include <windows.h>

#include <stdio.h>

 

#define BUFFER_SIZE 82

 

void main(int argc, WCHAR *argv[])

{

    HANDLE hFile;

    DWORD dwBytesRead = 0;

    char ReadBuffer[BUFFER_SIZE] = {0};

 

    printf("\n");

    // Verify the argument number

    if(argc != 2)

    {

        // The file must be available

        printf("ERROR:\tIncorrect number of arguments!\n\n");

        printf("%s <text_file_name>\n", argv[0]);

        return;

    }

 

    hFile = CreateFile(argv[1],                // file to open

                       GENERIC_READ,          // open for reading

                       FILE_SHARE_READ,   // share for reading

                       NULL,                  // default security

                       OPEN_EXISTING,         // existing file only

                       FILE_ATTRIBUTE_NORMAL, // normal file

                       NULL);                 // no attr. template

 

    if (hFile == INVALID_HANDLE_VALUE)

    {

        printf("Could not open %s file, error %d\n", argv[1], GetLastError());

        return;

    }

 

    // Read one character less than the buffer size to save room for

    // the terminating NULL character

    if( FALSE == ReadFile(hFile, ReadBuffer, BUFFER_SIZE-2, &dwBytesRead, NULL) )

    {

        printf("Could not read from %s, error %d\n", argv[1], GetLastError());

        CloseHandle(hFile);

        return;

    }

 

    if (dwBytesRead > 0)

    {

        ReadBuffer[dwBytesRead+1]='\0'; // NULL character

 

        printf("Text read from %s file, %d bytes: \n", argv[1], dwBytesRead);

        printf("%s\n", ReadBuffer);

    }

    else

    {

        printf("No data read from file %s\n", argv[1]);

    }

 

    CloseHandle(hFile);

}

 

Build and run the project. The output is expected having simple errors. Please rectify the error if you can.

 

Open a File for Reading program example: A sample console program output in action

 

The Unicode version can be found in the download page.

 

 

 

 

< Windows Disk 10 | Win32 Programming Index Page | Windows Disk Index | Windows Disk 12 >