c++ - "ReadFile Function"有没有办法让我不再声明要读取的特定字节数?

标签 c++ api readfile createfile writefile

我正在尝试使用 C++ 中的 Windows API 编写程序 目标是读取我创建的文本文件的内容,并能够使用按位 XOR(更改为小写和大写,反之亦然)操作内容,然后将操作的内容再次放入文本文件中。 这是我使用的程序的流程:

  1. 使用 CreateFile 打开文本文件。
  2. 将文本文件的内容放在创建的 malloc 上。
  3. 使用按位异或 (xor = 20) 处理文本文件的内容。
  4. 然后将异或值再次放入文本文件。

代码如下:

#include "pch.h"
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    HANDLE openFile;
    HANDLE ovewriFile;
    BOOL readwriFile;
    DWORD dwNoByteRead = 0;
    DWORD dwNoByteWritten = 0;
    char *strVal;
    //allocate memory
    strVal = (char*)malloc(sizeof(strVal));
    memset(strVal, '0x00', sizeof(strVal));

    //open a file
    openFile = CreateFile(L"C:\\Users\\John.Doe\\Documents\\test.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    //reading the content
    readwriFile = ReadFile(openFile, strVal, 34, &dwNoByteRead, NULL);
    cout << "original content is " << strVal << endl;

    CloseHandle(openFile);

    //manipulate data using xor
    for (int i = 0; i != strlen(strVal); i++) {
        if (strVal[i] == 0x20)
        {
            continue;
        }
        strVal[i] ^= 0x20;
    }
    cout << "xor value: " << strVal << endl;

    //overwrite a file
    ovewriFile = CreateFile(L"C:\\Users\\farrel.moje\\Documents\\test.txt", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    //write the content
    readwriFile = WriteFile(ovewriFile, strVal, 34, &dwNoByteWritten, NULL);

    //just a way to know if successful yung writefile
    if (readwriFile == FALSE) {

        cout << "ERROR WRITING " << GetLastError() << endl;
    }
    else {
        cout << "Success Overwrite " << endl;
    }
    cout << "Modified content " << strVal << endl;

    CloseHandle(ovewriFile);
    free(strVal);

    return 0;
}

这个程序正在运行,但是当我尝试将 nNumberOfBytesToRead(要读取的最大字节数)从 34 更改为 sizeof(strVal) 或 strlen(strVal)

readwriFile = ReadFile(openFile, strVal, 34, &dwNoByteRead, NULL);

但是使用sizeof和strlen,它并没有显示出文本文件的全部内容。 有没有办法不再声明要读取的特定字节数?

提前致谢!

最佳答案

删除你的

strVal = (char*)malloc(sizeof(strVal));
memset(strVal, '0x00', sizeof(strVal));

因为这是个坏主意。

更改一些代码以获取文件的大小:

openFile = CreateFile(L"C:\\Users\\John.Doe\\Documents\\test.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

LARGE_INTEGER fs;
GetFileSizeEx(openFile, &fs);
unsigned long long fSize = fs.QuadPart;
strVal = new char[fSize+1];
memset(strVal, 0, fSize+1);

//reading the content
readwriFile = ReadFile(openFile, strVal, fSize, &dwNoByteRead, NULL)

最后别忘了

delete []strVal;

而不是你的 free(strVal)

关于c++ - "ReadFile Function"有没有办法让我不再声明要读取的特定字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53295951/

相关文章:

c++ - 如何使用 Boost.Interprocess 将数据流式传输到其他应用程序?

java - Java API 中的正确返回值?

c++ - 使用 C++ 读取和拆分

c - 从文件读取特定字符串时程序执行中断

c++ - 输入 int 和 str 并将它们存储到两个单独的列表中

c++ - Visual Leak Detector 未发现泄漏 VS2013

Angular 6 ResponseContentType

react-native - React native 中的 react-native-fs 库是否有任何替代方案?

c++ - 不能使用 boost::shared_mutex

python - 如何在python脚本中发送post请求正文