为什么我用写文件写入 0 字节
Why Am I Writing 0 bytes with WriteFile?
我正在尝试制作一个程序,该程序在用户定义的目录中查找下一个.exe文件并向其写入一个标志(用于计算机安全类)。
它目前正在写入 0 字节,我不确定为什么。
这是我到目前为止的代码:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <Windows.h>
#include <fstream>
using namespace std;
typedef wchar_t wchar;
struct thePlague{
long int origBytes;
wchar_t flag[6];
};
void main() {
WIN32_FIND_DATA findFileData; //Windows structure that receives info about a found file or directory
HANDLE hFind = INVALID_HANDLE_VALUE; //Handle that holds whether or not a file was successfully found with FindNextFile
LARGE_INTEGER fileSize;
wchar szDir[MAX_PATH];
DWORD dwError = 0;
wchar directory[MAX_PATH];
wcout<<L"Please enter a directory: ";
wcin>>directory;
// Check that the input path plus 7 is not longer than MAX_PATH.
// 7 characters are for the "*.exe" plus NULL appended below.
if (wcslen(directory) > (MAX_PATH - 7)) {
wcout<<L"nDirectory is too longn";
return;
}
wcout<<L"Target directory is: "<<directory<<endl;
// Prepare string for use with FindFile functions. First, copy the
// string to a buffer, then append '*' to the directory name.
wcscpy(szDir, directory);
wcscat(szDir, L"\*.exe");
//Find the first file in the directory
hFind = FindFirstFile(szDir, &findFileData);
if (hFind == INVALID_HANDLE_VALUE) {
wcout<<L"FindFirstFile Failed"<<GetLastError()<<endl;
return;
}
do {
fileSize.LowPart = findFileData.nFileSizeLow;
fileSize.HighPart = findFileData.nFileSizeHigh;
wcout<<findFileData.cFileName<<L" "<<fileSize.QuadPart<<L" bytesn";
DWORD dwBytesWritten = 0; //# of bytes written by WriteFile
wchar_t *buffer = (wchar *)malloc(sizeof(thePlague)); //A buffer the size of our struct (infection flag)
wofstream writeToBuffer(buffer, ios::binary); //Open the buffer for writing
thePlague newVictim; //New infection flag
writeToBuffer.write((const wchar_t*)&newVictim, sizeof(thePlague));
writeToBuffer.close();
DWORD dwErrorFlag;
dwErrorFlag = WriteFile(hFind, //The handle holding the file we are writing to
buffer, //The buffer holding what we want to write to the file, in this case a struct
sizeof(thePlague), //How many bytes to write to the file
&dwBytesWritten, //Where to store the # of bytes successfully written
NULL //Where to write to incase of overlap
);
if (dwErrorFlag == false)
wcout<<L"Error was: "<<GetLastError()<<endl;
wcout<<L"# of bytes written: "<<dwBytesWritten<<endl;
} while(FindNextFile(hFind, &findFileData) != 0);
FindClose(hFind);
return;
}
当我运行这个时,我得到:
Please enter a directory: C:Infect
Target directory is: C:Infect
hello.exe 65536 bytes
Error was: 6
# of bytes written: 0
Press any key to continue . . .
我知道这是一个ERROR_INVALID_HANDLE,但我该如何解决这个问题?
您需要先打开文件,然后再写入它。您的HANDLE hFind
只是迭代目录/文件的句柄,而不是文件的打开句柄。
使用 CreateFile() 打开文件
> FindFirstFile
返回搜索句柄,但WriteFile
需要打开的文件句柄。使用 CreateFile
打开文件,然后使用 WriteFile
写入文件。
相关文章:
- 在UNIX系统中使用DIR查找文件的字节大小
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 如何在文件中查找字节序列
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 如何使用 fprintf 将连续的 512 字节保存到文件中
- 用于 progmem 的C++和头文件压缩的 Web 文件字节数组
- 为什么未关闭的文件大小为 4 字节
- 我能确定从文件中读取的 32 字节二进制数据等于 256 位吗?
- Ascii .txt文件到字节数组 - C++
- 多部分/表单数据丢失大文件的字节
- 我该如何循环遍历我的数组(缓冲区——包含一个文本文件),并将其打印成30字节的块
- 程序错误地读取了二进制文件的一个字节
- 如何从 C 或 C++ 格式的文件中删除字节?
- C++ - 将任何文件的字节读取到无符号字符数组中
- ofstream将额外的零字节写入Unix服务器上的文件
- 传输文件一次又一次地发送相同的字节
- 编码大于原始文本:如何获取零和一的字符串并将它们作为实际字节写入文件
- 查找二进制文件中字节序列的所有匹配项
- C++ 使用单个输入字节文件的不同部分实例化堆栈上的成员类