简体   繁体   中英

ReadFile lpBuffer parameter

I am using ReadFile to read a simple string that I wrote to a file using WriteFile.

Have a simple string: "Test string, testing windows functions".

Used WriteFile to write that to a file.

Now I want to use ReadFile to confirm that it was written to the file. I need to compare what I read to the original string above. To Read from the file I have

DWORD dwBytesRead;
char buff[128];
if(!ReadFile(hFile, buff, 128, &dwBytesRead, NULL))
    //Fail

The function returns true so it is reading from the file. The problem is buff is full of just ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ. I've never come across LPVOID before so I don't know if it is something there or what. Is there a way to do this string comparison?

EDIT: The code i use to write to the file is quite simple:

if(!WriteFile(hFile, sentence.c_str(), sentence.length(), &bytesWritten, NULL))
{
    //FAIL
}

The file pointer needs rewound after the WriteFile() and before the ReadFile() . As it stands, ReadFile() does not fail but reads zero bytes thus buff is unchanged. As buff is uninitialised it contains junk. To rewind the file pointer to the beginning of the file use SetFilePointer() :

#include <windows.h>
#include <iostream>
#include <string>

int main()
{
    HANDLE hFile = CreateFile ("myfile.txt",
                               GENERIC_WRITE | GENERIC_READ,
                               0,
                               NULL,
                               OPEN_EXISTING,
                               FILE_ATTRIBUTE_NORMAL,
                               NULL);
    if (hFile)
    {
        std::string sentence("a test");
        DWORD bytesWritten;
        if (WriteFile(hFile,
                      sentence.c_str(),
                      sentence.length(),
                      &bytesWritten,
                      NULL))
        {
            if (INVALID_SET_FILE_POINTER != SetFilePointer(hFile,
                                                           0,
                                                           0,
                                                           FILE_BEGIN))
            {
                char buf[128] = { 0 }; /* Initialise 'buf'. */
                DWORD bytesRead;

                /* Read one less char into 'buf' to ensure null termination. */
                if (ReadFile(hFile, buf, 127, &bytesRead, NULL))
                {
                    std::cout << "[" << buf << "]\n";
                }
                else
                {
                    std::cerr << "Failed to ReadFile: " <<
                        GetLastError() << "\n";
                }
            }
            else
            {
                std::cerr << "Failed to SetFilePointer: " <<
                    GetLastError() << "\n";
            }

        }
        else
        {
            std::cerr << "Failed to WriteFile: " << GetLastError() << "\n";
        }

        CloseHandle(hFile);
    }
    else
    {
        std::cerr << "Failed to open file: " << GetLastError() << "\n";
    }

    return 0;
}

The function returns true so it is reading from the file. The problem is buff is full of just ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ.

ReadFile only fills the buffer up to the value of dwBytesRead . If you're trying to work with a string, you'll have to null terminate it yourself after ReadFile returns:

buff [dwBytesRead] = 0;

You should not use 128 as the nNumberOfBytesToRead , since you can get out of bounds while printing the string (or otherwise considering buff as a 0-terminated string). Also check dwBytesRead if it really reads that many bytes, and 0-terminate the string as suggested by @James McLaughlin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM