我正在 SD 卡上写入/读取原始数据。编写代码的工作量约为。 4700 block 并在此限制后失败。这是代码:
//Data to be written
uint8_t* sessions;
sessions = (uint8_t *) malloc(2048*sizeof(uint8_t));
unsigned int i;
for(i=0;i<(2048*sizeof(uint8_t));i++) sessions[i]=8;
DWORD dwWrite;
HANDLE hDisk=CreateFileA("\\\\.\\K:", // drive to open = SD CARD
GENERIC_WRITE, // access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
FILE_FLAG_NO_BUFFERING, // file attributes
NULL); // do not copy file attributes
if(hDisk==INVALID_HANDLE_VALUE)
{
CloseHandle(hDisk);
printf("ERROR opening the file !!! ");
}
DWORD dwPtr = SetFilePointer(hDisk,10000*512,0,FILE_BEGIN); //4700 OK
if (dwPtr == INVALID_SET_FILE_POINTER) // Test for failure
{
printf("CANNOT move the file pointer !!! ");
}
//Try using this structure but same results: CAN BE IGNORED
OVERLAPPED osWrite = {0,0,0};
memset(&osWrite, 0, sizeof(osWrite));
osWrite.Offset = 10000*512; //4700 OK
osWrite.hEvent = CreateEvent(FALSE, FALSE, FALSE, FALSE);
if( FALSE == WriteFile(hDisk,sessions,2048,&dwWrite,&osWrite) ){
printf("CANNOT write data to the SD card!!! %lu",dwWrite);
}else{
printf("Written %lu on SD card",dwWrite);
}
CloseHandle(hDisk);
问题出在函数“Writefile”(windows.h) 上。如果 block 数小于 4700。一切都很好(数据写入 SD 卡)但如果 block 数比方说 5000 或 10000,函数将失败“写入 0”。
请注意,如果没有 FILE_FLAG_NO_BUFFERING,则无法打开驱动器(SD 卡)。 “OVERLAPPED”是使其工作的失败尝试,不使用它 (WriteFile(hDisk,sessions,2048,&dwWrite,NULL) ) 会导致相同的行为。 “SetFilePointer”也适用于高于 4700 的 block 。还测试了 2 种不同的 SD 卡。我在 Windows 10 上。
关于发生了什么的任何提示?
感谢您的参与
最佳答案
来自documentation for WriteFile :
A write on a volume handle will succeed if the volume does not have a mounted file system, or if one of the following conditions is true:
The sectors to be written to are boot sectors.
The sectors to be written to reside outside of file system space.
You have explicitly locked or dismounted the volume by using
FSCTL_LOCK_VOLUME
orFSCTL_DISMOUNT_VOLUME
.The volume has no actual file system. (In other words, it has a RAW file system mounted.)
您可以写入前几兆字节,因为(出于历史原因)文件系统不使用该空间。为了写入卷的其余部分,您首先必须使用 FSCTL_LOCK_VOLUME
control code 锁定卷。 .
关于c++ - WriteFile 失败超过 4700 个 block (SD 卡原始写入/窗口),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44138716/