This code should read in some data from sector zero off of a USB flash drive, then print out some of the data. However when I run it there is nothing that was put in the buffer.

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();
}   

The variable ReadSect is given a proper value.

Lines 12 and 13 become 0 << 9 and 0 >> 23 which both equal 0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.