C read binary stdin

前端 未结 8 2196
难免孤独
难免孤独 2020-11-30 04:51

I\'m trying to build an instruction pipeline simulator and I\'m having a lot of trouble getting started. What I need to do is read binary from stdin, and then store it in me

相关标签:
8条回答
  • 2020-11-30 05:43

    For Windows, this Microsoft _setmode example specifically shows how to change stdin to binary mode:

    // crt_setmode.c
    // This program uses _setmode to change
    // stdin from text mode to binary mode.
    
    #include <stdio.h>
    #include <fcntl.h>
    #include <io.h>
    
    int main( void )
    {
       int result;
    
       // Set "stdin" to have binary mode:
       result = _setmode( _fileno( stdin ), _O_BINARY );
       if( result == -1 )
          perror( "Cannot set mode" );
       else
          printf( "'stdin' successfully changed to binary mode\n" );
    }
    
    0 讨论(0)
  • 2020-11-30 05:46

    Consider using SET_BINARY_MODE macro and setmode:

    #ifdef _WIN32
    # include <io.h>
    # include <fcntl.h>
    # define SET_BINARY_MODE(handle) setmode(handle, O_BINARY)
    #else
    # define SET_BINARY_MODE(handle) ((void)0)
    #endif
    

    More details about SET_BINARY_MODE macro here: "Handling binary files via standard I/O"

    More details about setmode here: "_setmode"

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