简体   繁体   English

ReadFile lpBuffer参数

[英]ReadFile lpBuffer parameter

I am using ReadFile to read a simple string that I wrote to a file using WriteFile. 我正在使用ReadFile读取我使用WriteFile写入文件的简单字符串。

Have a simple string: "Test string, testing windows functions". 有一个简单的字符串:“测试字符串,测试Windows函数”。

Used WriteFile to write that to a file. 使用WriteFile将其写入文件。

Now I want to use ReadFile to confirm that it was written to the file. 现在,我想使用ReadFile确认它已写入文件中。 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. 该函数返回true,因此正在从文件读取。 The problem is buff is full of just ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ. 问题是,仅仅只是一个完整的buff就被buff淹没了。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 I've never come across LPVOID before so I don't know if it is something there or what. 我之前从未接触过LPVOID,所以我不知道它是否存在。 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() . 文件指针需要在WriteFile()ReadFile()之前ReadFile() As it stands, ReadFile() does not fail but reads zero bytes thus buff is unchanged. 就目前而言, ReadFile()不会失败,但是会读取零字节,因此buff不变。 As buff is uninitialised it contains junk. 由于buff未初始化,因此包含垃圾。 To rewind the file pointer to the beginning of the file use SetFilePointer() : 要将文件指针倒退到文件的开头,请使用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. 该函数返回true,因此正在从文件读取。 The problem is buff is full of just ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ. 问题是,仅仅只是一个完整的buff就被buff淹没了。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

ReadFile only fills the buffer up to the value of dwBytesRead . ReadFile仅将缓冲区填充到dwBytesRead的值。 If you're trying to work with a string, you'll have to null terminate it yourself after ReadFile returns: 如果您尝试使用字符串,则必须在ReadFile返回后ReadFile终止它:

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). 您不应将128用作nNumberOfBytesToRead ,因为您在打印字符串时可能会越界(或者将buff视为以0结尾的字符串)。 Also check dwBytesRead if it really reads that many bytes, and 0-terminate the string as suggested by @James McLaughlin. 还要检查dwBytesRead是否确实读取了那么多字节,并按@James McLaughlin的建议以0终止该字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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