我正在SD/SD卡上写入/读取原始数据。编写代码的工作时间约为4700块,并在此限制后失败。这里是代码:对于> 4700块(SD卡原始写入/窗口),WriteFile失败
//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)。如果块号小于4700,则一切正常(数据写入SD卡),但如果块号为5000或10000,则该功能将失败“写入0”。
注意,如果没有FILE_FLAG_NO_BUFFERING,没办法打开驱动器(SD卡)。 “重叠”是一个失败的尝试,使其工作,不使用它(WriteFile(hDisk,会话,2048,& dwWrite,NULL))导致相同的行为。 “SetFilePointer”也适用于高于4700的模块。还测试了2个不同的SD卡。我在Windows 10上。
有什么暗示发生了什么?
谢谢您的输入
GetLastError在什么? – RbMm
你真的想写逻辑驱动器(SD卡上的分区),而不是物理驱动器(SD卡本身)? –