WriteFile 返回错误 1784
我正在创建一个程序来使用虚拟文件系统填充磁盘。
目前,我正在使用 WriteFile
写入可变大小的文件。
WriteFile(hFile, FileData, i * 1024, &dwWrote, NULL);
err = GetLastError();
错误返回#1784,翻译为
提供的用户缓冲区对于请求的操作无效。 ERROR_INVALID_USER_BUFFER
因此,对于前 24 个文件,写入操作有效。对于文件 #25,写入操作失败。 文件仍会创建,但 WriteFile 函数不会填充文件。
关于如何克服ERROR_INVALID_USER_BUFFER
有什么想法吗?
我能找到的所有关于该错误的参考都仅限于崩溃的程序,我无法弄清楚它与我遇到的问题有何关系。
编辑:
FileData = (char *) malloc(sizeof(char) * (size_t)k * 1024);
memset(FileData, 245, sizeof(char) * (size_t)k * 1024);
FileData 设置并分配给最大预期缓冲区的大小。 i 是迭代的循环变量,直到递增到最大大小 (k)。
I am creating a program to populate a disk with a dummy file system.
Currently, I am writing files of variable sizes using WriteFile
.
WriteFile(hFile, FileData, i * 1024, &dwWrote, NULL);
err = GetLastError();
err returns #1784 which translates to
The supplied user buffer is not valid for the requested operation. ERROR_INVALID_USER_BUFFER
So for the first 24 files, the write operation works. For file #25 on, the write operation fails.
The files are still created but the WriteFile function does not populate the files.
Any ideas on how to get past ERROR_INVALID_USER_BUFFER
?
Every reference I can find to the error is limited to crashing programs and I cannot figure out how it relates to the issue I am experiencing.
EDIT:
FileData = (char *) malloc(sizeof(char) * (size_t)k * 1024);
memset(FileData, 245, sizeof(char) * (size_t)k * 1024);
FileData is set and allocated to the size of the maximum anticipate buffer.
i is the loop variable that iterates until it increments to the Maximum Size (k).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是
FileData
不够大,无法从中写入i * 1024
字节。i
是文件列表的循环控制变量吗?如果是这样,您需要在循环访问文件时写入缓冲区FileData
一次增加 1K。这是一个不寻常的结构。你确定这里的逻辑是正确的吗?发布更多代码(特别是
FileData
和i
的所有用法),以提高答案的准确性。请注意,您不应该总是在此处检查
GetLastError
- 您需要先检查WriteFile
的返回代码,然后才能相信它有意义。否则,您可能会从代码的某些不相关部分中发现错误 - 无论最后失败的是什么。My guess is that
FileData
is not large enough for you to writei * 1024
bytes from it. Isi
the loop control variable for your list of files? If so, you need the write bufferFileData
to grow 1K at a time as you loop through your files.This is an unusual construct. Are you sure the logic is correct here? Post more code (specifically, all usage of
FileData
andi
) for better accuracy in the answers.Note that you should not always be checking
GetLastError
here - you need to checkWriteFile
's return code before you rely on that being meaningful. Otherwise you could be picking up an error from some unrelated part of your code - whatever failed last.我收到错误 = 1784,这是因为我在没有指定记录大小的情况下打开了文件,然后对文件进行了块读取。
应该是
I got a Error = 1784 and it was because I opened the file without specifying the size of records and then did block reads on the file.
Should be