c++ ReadFile()缓冲区使用printf()打印垃圾?

js4nwp54  于 2023-05-02  发布在  其他
关注(0)|答案(3)|浏览(159)

尝试从现有文件创建句柄(测试。txt),读取数据到buffer并使用printf打印buffer。
一切显然工作正常-没有错误,读取的字节数是正确的。但是当我用printf打印缓冲区时,一些垃圾打印在数据从txt文件到屏幕之后:

代码为:

#include <windows.h>
#include <stdio.h>

WCHAR input_file[9] = L"test.txt";
LPCWSTR p_input_file = input_file;

#define BUFFER_SIZE 24

int main()
{
    HANDLE hFile = NULL;
    CHAR inBuffer[BUFFER_SIZE];
    LPVOID pbuffer = inBuffer;
    DWORD nNumberOfBytesToRead = BUFFER_SIZE;
    DWORD nNumberOfBytesRead;

    hFile = CreateFile(input_file,               
        GENERIC_READ,          
        FILE_SHARE_READ,       
        NULL,                  
        OPEN_EXISTING,         
        FILE_ATTRIBUTE_NORMAL, 
        NULL);                 

    BOOL reading_result = ReadFile(hFile,
        pbuffer,
        nNumberOfBytesToRead,
        &nNumberOfBytesRead,
        NULL);

    int error_code_value = GetLastError();

    if (error_code_value == 0)
    {
        printf("Work properly. The text is: %s \n", inBuffer);
    }
    else if (error_code_value == ERROR_INVALID_HANDLE)
    {
        printf("Handle is loaded in invalid way.\n");
    }
    else
    {
        printf("Worked wrong. error code: %d. \n", GetLastError());
    }

    CloseHandle(hFile);
}
de90aj5v

de90aj5v1#

printf with %s打印从您所说的地址开始的字符,直到找到空字符。
ReadFile不会在数组中放置空字符(为什么会这样?))你也不知道,所以没有。
您知道读取了多少字节,因为它在变量nNumberOfBytesRead中。您可以选择打印那么多字节by using %.*s and also telling printf the number of bytes,也可以选择在读取的所有字符之后在数组中存储一个空字符。在第二种情况下,必须确保数组中有用于读取字符和空字符的空间。如果数组有24个字符,则只能读取23个字符,然后将最后一个字符设为null。

deyfvvtc

deyfvvtc2#

你想要这个(评论中的解释):

...
int main()
{
  HANDLE hFile = NULL;
  CHAR inBuffer[BUFFER_SIZE + 1];  // + 1 for the null character
  LPVOID pbuffer = inBuffer;
  DWORD nNumberOfBytesToRead = BUFFER_SIZE;
  DWORD nNumberOfBytesRead;

  hFile = CreateFile(input_file,
    GENERIC_READ,
    FILE_SHARE_READ,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL);

  if (hFile == INVALID_HANDLE_VALUE)  // first thing to do
  {
    printf("CreateFile failed: error code = %d.\n", GetLastError());
  }
  else
  {
    BOOL reading_result = ReadFile(hFile,
      pbuffer,
      nNumberOfBytesToRead,
      &nNumberOfBytesRead,
      NULL);

    if (reading_result)  // you need to check reading_result
    {
      ((char*)pbuffer)[nNumberOfBytesRead] = 0;   // put a null terminator
      printf("Works properly. The text is: %s \n", inBuffer);
    }
    else
    {
      printf("Worked wrong. error code: %d.\n", GetLastError());
    }

    CloseHandle(hFile);
  }
}
rkkpypqq

rkkpypqq3#

尝试使用null初始化inBuffer:

#include <windows.h>
#include <stdio.h>

WCHAR input_file[9] = L"test.txt";
LPCWSTR p_input_file = input_file;

#define BUFFER_SIZE 24

int main()
{
    HANDLE hFile = NULL;
    CHAR inBuffer[BUFFER_SIZE];
    LPVOID pbuffer = inBuffer;
    DWORD nNumberOfBytesToRead = BUFFER_SIZE;
    DWORD nNumberOfBytesRead;
    memset(inBuffer, 0, sizeof(inBuffer)); // Initialize with null / 0

    hFile = CreateFile(input_file,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);

    BOOL reading_result = ReadFile(hFile,
        pbuffer,
        nNumberOfBytesToRead,
        &nNumberOfBytesRead,
        NULL);

    int error_code_value = GetLastError();

    if (error_code_value == 0) {
        printf("Work properly. The text is: %s \n", inBuffer);
    } else if (error_code_value == ERROR_INVALID_HANDLE) {
        printf("Handle is loaded in invalid way.\n");
    } else {
        printf("Worked wrong. error code: %d. \n", GetLastError());
    }

    CloseHandle(hFile);
    return 0;
}

初始化NULL

未初始化NULL

相关问题