0

你好我正在创建一个文件加密代码我读取一个文件,我需要将我正在读取的文件转换为 BYTE* 我尝试搜索但每次我得到"CL.exit"。这就是我阅读文件的方式。

HANDLE getFile = createFile();
    DWORD reciveBytes = 0;
    //If it's byte or kilobyte the size of the buffer will be 1024.
    //If it's megabytes or gigabyte the size of the buffer will be 4096.
    const DWORD Buffersize = 66232; // gave me warning for 1024
    DWORD buffer[Buffersize];
    string fileInput;
    if (ReadFile(
        getFile,
        buffer,
        Buffersize,
        &reciveBytes,
        NULL
    )) {
    }
    else {
        cout << "Faild!" << endl;
        cout << GetLastError() << endl;
    }
    /*
    for (unsigned int i = 0; i < reciveBytes; i++) {
        if (buffer[i] != '\0') {
            fileInput = fileInput + buffer[i];
        }
    }
    */
    return buffer[reciveBytes];

现在,我需要做的是将返回类型转换为 BYTE*,这样我就可以执行以下操作: BYTE* protect = (BYTE*)"Hello world!"; 这是 createFile():

HANDLE getFile = CreateFileA(
        fileName,
        GENERIC_READ,
        0,
        NULL,
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, 
        NULL
    );
4

1 回答 1

0
#include <windows.h>
#include <iostream>
HANDLE createFile()
{
    HANDLE getFile = CreateFileA(
        "xxx.txt",
        GENERIC_READ,
        0,
        NULL,
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
        return getFile;
}
BYTE* memoryfromfile()
{
    HANDLE getFile = createFile();
    DWORD reciveBytes = 0;
    //If it's byte or kilobyte the size of the buffer will be 1024.
    //If it's megabytes or gigabyte the size of the buffer will be 4096.
    const DWORD Buffersize = 66232; // gave me warning for 1024
    BYTE* memory = new BYTE[Buffersize];
    memset(memory, 0, Buffersize);
    std::string fileInput;
    if (ReadFile(
        getFile,
        memory,
        Buffersize,
        &reciveBytes,
        NULL
    )) {
    }
    else {
        std::cout << GetLastError() << std::endl;
        std::cout << "Faild!" << std::endl;
    }
    /*
    for (unsigned int i = 0; i < reciveBytes; i++) {
        if (buffer[i] != '\0') {
            fileInput = fileInput + buffer[i];
        }
    }
    */
    return memory;
}
int main()
{
    BYTE * temp = memoryfromfile();
    std::cout << "temp = " << temp << std::endl;
    delete temp;
    system("pause");
    return 0;
}
于 2019-09-20T09:48:01.483 回答