Windows physical drive access fopen and fseek

后端 未结 1 693
太阳男子
太阳男子 2021-01-25 00:03

I\'m currently trying to access a physical hard disk as a stream of binary data in C. I\'ve mounted an image (.img), and it\'s readable from the OS (Win 7).

My C progra

相关标签:
1条回答
  • 2021-01-25 00:21

    Quote from msdn:

    If successful, fseek returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined. If stream is a null pointer, or if origin is not one of allowed values described below, fseek invoke the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions set errno to EINVAL and return -1.

    Section 7.21.9.2 of the C standard [ISO/IEC 9899:2011] specifies the following behavior for fseek() when opening a binary file in binary mode:

    A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END.

    In addition, footnote 268 of Section 7.21.3 has this to say:

    Setting the file position indicator to end-of-file, as with fseek(file, 0, SEEK_END), has undefined behavior for a binary stream (because of possible trailing null characters) or for any stream with state-dependent encoding that does not assuredly end in the initial shift state.

    A lot of low-level stuff matters, like sector alignment, file systems,etc. which needs a specific driver program to help navigating through the drives also need to be considered.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题