- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只是想将卷句柄的文件指针从FILE_END
向后移动。不幸的是,无论我传递给它的任何输入值如何,操作都会失败!
我使用以下代码:
HANDLE vol_handle = CreateFile ("\\\\.\\C:",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (vol_handle == INVALID_HANDLE_VALUE)
{
printf("Failed to create volume handle!\n");
print_last_error(GetLastError());
return ERROR;
}
printf("Handle acquired for volume %s \n", VOLUME);
long int dist = 512000; // 500 KB forwards
SetLastError(0);
int result = SetFilePointer (vol_handle, dist, NULL, FILE_BEGIN);
if (result == INVALID_SET_FILE_POINTER)
{
printf("SetFilePointer (BEGIN) FAILED!\n");
print_last_error(GetLastError());
}
else
printf("SetFilePointer (BEGIN) SUCCESS!\n");
dist = -5120; // 50 KB backwards
SetLastError(0);
result = SetFilePointer (vol_handle, dist, NULL, FILE_END);
if (result == INVALID_SET_FILE_POINTER)
{
printf("SetFilePointer (END) FAILED!\n");
print_last_error(GetLastError());
}
else
printf("SetFilePointer (END) SUCCESS!\n");
SetLastError(0);
result = SetFilePointer (vol_handle, dist, NULL, FILE_CURRENT);
if (result == INVALID_SET_FILE_POINTER)
{
printf("SetFilePointer (CURRENT) FAILED!\n", offset_low_part);
print_last_error(GetLastError());
}
else
printf("SetFilePointer (CURRENT) SUCCESS!\n");
输出:
Handle acquired for volume \\.\C: SetFilePointer (BEGIN) SUCCESS! SetFilePointer (END) FAILED! Error 0x57: The parameter is incorrect. SetFilePointer (CURRENT) SUCCESS!
As you can see, the seek works fine for FILE_BEGIN
and FILE_CURRENT
, but it always fails for FILE_END
with error code 0x57
(87 in decimal) saying:
The parameter is incorrect
Note that changing dist
to a positive value doesn't change the situation.
What am I missing here?
( NOTE: Testing on Windows-5.1.2600 (XP SP3) in latest Code::Blocks IDE 16.01 with MinGW compiler, and Administrator privileges )
NOTE: This is absolutely not a duplicate of this question, since I am not accessing physical disk (volume access is different from physical disk) and the distance value is a multiple of 512 bytes (the logical sector size). If the distance length was incorrect, then the other seeks (BEGIN and CURRENT) would have failed as well, yet they don't.
Based on Rohans' answer, I performed another check with the following code:
long long int offset_64 = 5120000 // 5000 KB distance;
long int offset_low_part = -(0xffffffff & offset_64);
long int offset_hig_part = offset_64 >> 32;
printf("offset_64 = %lld\n", offset_64);
printf("low_part = %ld\n", offset_low_part);
printf("high_part = %ld\n\n", offset_hig_part);
// Get volume handle using CreateFile(...) exactly as before;
SetLastError(0);
int result = SetFilePointer (vol_handle, offset_low_part, &offset_hig_part, FILE_END);
if (result == INVALID_SET_FILE_POINTER)
{
print_last_error(GetLastError());
printf("Failed to set file pointer!\n");
printf("high_part after SetFilePointer = %ld\n", offset_hig_part);
return ERROR;
}
虽然我相信Rohans的观点很重要,但它仍然没有解决问题:
offset_64 = 5120000
low_part = -5120000
high_part = 0
Handle acquired for volume \\.\C:
Error 0x57: The parameter is incorrect.
Failed to set file pointer!
high_part after SetFilePointer = 0
对 low_part
使用正值不会改变这种情况。
根据下面 David Heffernan 的评论,我承认使用 SetFilePointerEx
可以使这段代码更短、更容易,但结果仍然相同,并且操作无法从 FILE_END 中查找
code> 在音量 handle 上。
最佳答案
这并不奇怪。卷不是文件,因此它没有文件结束位置。
Windows当然可以使用卷长度作为文件末尾位置,但没有特别的理由让它处理这种特殊情况。
您必须自己计算偏移量。
关于c++ - 对于卷句柄,来自 FILE_END 的 SetFilePointer 始终失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37738926/
首先,如果我说了一些愚蠢的话,请原谅我,我不是 IT 人员,而是电子工程师,但我被分配到需要更多技能的工作。 我需要写入和读取 SD 卡中的物理扇区。我已经用 C++ 完成了它,但主要应用程序是用 C
考虑这个程序: #include #include int main(int argc, char** argv) { if (argc != 2) return 1; HANDL
大家好,如果我想使用 测试 EOF SetFilePointer(fi1, 0, NULL, FILE_CURRENT) != INVALID_SET_FILE_POINTER 还是? SetFile
我只是想将卷句柄的文件指针从FILE_END向后移动。不幸的是,无论我传递给它的任何输入值如何,操作都会失败! 我使用以下代码: HANDLE vol_handle = CreateFile ("\\
If SetFilePointer(hFile, 0&, ByVal 0&, FILE_END) _ = INVALID_SET_FILE_POINTER Then 此调用引发了 HBF_SE
我是一名优秀的程序员,十分优秀!