Naseef Chowdhury
Naseef Chowdhury

Reputation: 2464

Unable to Read from File while using ReadFIle() function in C++

I am facing some issues while reading data from file using ReadFile() function of C++ (Microsoft specific probably).

Here is my code

Write On File

void ClientA::SharePublicKey()
{
    printf("Sharing Public Key\n");
    HANDLE hFile = NULL;

    hFile = CreateFile(TEXT("D:\\My_Proj\\shared\\PublicKeyB.txt"),                // name of the write
                       GENERIC_WRITE,          // open for writing
                       FILE_SHARE_WRITE,                      // do not share
                       NULL,                   // default security
                       CREATE_NEW,             // create new file only
                       FILE_ATTRIBUTE_NORMAL,  // normal file
                       NULL);                  // no attr. template

    if (hFile == INVALID_HANDLE_VALUE) 
    { 
        //DisplayError(TEXT("CreateFile"));
        //_tprintf(TEXT("Terminal failure: Unable to open file \"%s\" for write.\n"), argv[1]);
        return;
    }

   // _tprintf(TEXT("Writing %d bytes to %s.\n"), dwBytesToWrite, argv[1]);

    bool bErrorFlag = WriteFile( 
                    hFile,           // open file handle
                    pbPublicKey,      // start of data to write
                    dwPublicKeyLen,  // number of bytes to write
                    &lpNumberOfBytesWritten, // number of bytes that were written
                    NULL);            // no overlapped structure

    if (FALSE == bErrorFlag)
    {
       // DisplayError(TEXT("WriteFile"));
        printf("Terminal failure: Unable to write to file.\n");
        return;
    }
    else
    {
        if (lpNumberOfBytesWritten != dwPublicKeyLen)
        {
            // This is an error because a synchronous write that results in
            // success (WriteFile returns TRUE) should write all data as
            // requested. This would not necessarily be the case for
            // asynchronous writes.
            printf("Error: dwBytesWritten != dwBytesToWrite\n");
        }
        else
        {
          //  _tprintf(TEXT("Wrote %d bytes to %s successfully.\n"), dwBytesWritten, argv[1]);
        }
    }

    CloseHandle(hFile);
}

Read That File

void ClientA::ReadPublicKeyOfOtherPeer()
{
    HANDLE hFile = NULL; 
    DWORD  dwBytesRead = 0;
    BYTE*   ReadBuffer = NULL;
    OVERLAPPED ol = {0};


    hFile = CreateFile(TEXT("D:\\My_Proj\\shared\\PublicKeyB.txt"),               // 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 | FILE_FLAG_OVERLAPPED, // normal file
                       NULL                 // no attr. template
                       );

    if (hFile == INVALID_HANDLE_VALUE) 
    { 
        _tprintf(TEXT("CreateFile\n"));
        _tprintf(TEXT("Terminal failure: unable to open file \"%s\" for read.\n"));
        printf("Error %x\n", GetLastError());
        return; 
    }



    if( FALSE == ReadFile(hFile, ReadBuffer, dwPublicKeyLen, &lpNumberOfBytesWritten, &ol) )
    {
       // DisplayError(TEXT("ReadFile"));
        printf("Terminal failure: Unable to read from file.\n GetLastError=%08x\n", GetLastError());
        CloseHandle(hFile);
        return;
    }


    if (dwBytesRead > 0 && dwBytesRead <= dwPublicKeyLen-1)
    {
        ReadBuffer[dwBytesRead]='\0'; // NULL character

        //_tprintf(TEXT("Data read from %s (%d bytes): \n"), argv[1], dwBytesRead);
        printf("%s\n", ReadBuffer);
    }
    else if (dwBytesRead == 0)
    {
        //_tprintf(TEXT("No data read from file %s\n"), argv[1]);
    }
    else
    {
       // printf("\n ** Unexpected value for dwBytesRead ** \n");
    }

    retrievedPublicByteArray = ReadBuffer;




    CloseHandle(hFile);
}

By SharePublicKey method I am saving the data in a file. And I have checked that it successfully saves data on the file and the data on the files are seems to be valid.

And by ReadPublicKeyOfOtherPeer method I am reading the file which was previously saved. But reading is not successful In out put I found the following line -

Terminal failure: Unable to read from file. GetLastError=000003e6

Upvotes: 0

Views: 1947

Answers (1)

ScottMcP-MVP
ScottMcP-MVP

Reputation: 10425

You are passing uninitialized pointer ReadBuffer to ReadFile. You need a buffer that is large enough to receive the results.

Upvotes: 1

Related Questions