在USB设备上使用readFile时为空缓冲区

时间:2012-10-31 17:54:55

标签: c++ usb readfile createfile

此代码应从USB闪存驱动器的扇区零读取一些数据,然后打印出一些数据。但是当我运行它时,没有任何东西放在缓冲区中。

int _tmain(int argc, _TCHAR* argv[])
{
    int ReadSect = 0;
    char diskname[60];
    sprintf_s(diskname, "\\\\.\\K:", 'K');

    HANDLE DiskHandle = CreateFile((LPCTSTR) diskname , GENERIC_READ,FILE_SHARE_READ,     NULL, OPEN_EXISTING, 0, NULL);
        // set up the offsets for the addresses passed to setFilePointer, and ReadFile
    long lo_offset, hi_offset;
    LPDWORD temp = 0;
    lo_offset = ReadSect << 9;
    hi_offset = ReadSect >> 23;
    (void) SetFilePointer( DiskHandle, 0, &hi_offset, FILE_BEGIN);

    char buf[1024];
    for(int i = 0; i<512;i++) {
        buf[i]='X'; 
    }

    if (! ReadFile(DiskHandle, buf, 512, (LPDWORD)&lo_offset, NULL) )  {
        printf("error reading sector %ld\n", ReadSect);
    CloseHandle(DiskHandle);
    }
    for(int i = 0; i<512;i++) {
        cout<<(char)buf[i];
    }
    cout<<endl<<endl<<endl;
    getchar();
}   

0 个答案:

没有答案