Thread: Binary I/O with cin and cout

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    Binary I/O with cin and cout

    [15.13] How can I "reopen" std::cin and std::cout in binary mode?

    This is implementation dependent. Check with your compiler's documentation.

    For example, suppose you want to do binary I/O using std::cin and std::cout.

    Unfortunately there is no standard way to cause std::cin, std::cout, and/or std::cerr to be opened in binary mode. Closing the streams and attempting to reopen them in binary mode might have unexpected or undesirable results.

    On systems where it makes a difference, the implementation might provide a way to make them binary streams, but you would have to check the implementation specifics to find out.
    Is it talking about using cin and cout like cin>>x, cout<<x so that it outputs in binary instead of ASCII text?
    Because my understanding is that write() and read() can do the job.

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Is it talking about using cin and cout like cin>>x, cout<<x so that it outputs in binary instead of ASCII text?
    Because my understanding is that write() and read() can do the job.
    It depends on what you mean by "binary".

    If you are talking about base-2, then you can use strtol() to convert a string of ASCII ones & zeros to a long int. (Actually, strtol() can convert any base from base-2 to base-36.)
    Code:
    const int MAX_BITS = 8;     // Limit to one-byte to make binary readable
    int Base, x;
    char InputString[40];
    char *pEnd = NULL;          // Required for Strtol()
    
    cout << "Base? (2-36, 0 to exit) " ;
    cin >> Base;
           
    cout << "Number? "
    cin >> InputString;   // C-Style null-terminated string
    x = (int)strtol(InputString, &pEnd, Base);     // String to long (typecast to int)

    And, you can use <bitset> with cout to display a number in binary (base-2).
    Code:
    cout << "\t" << dec << x << "\t\t Decimal" << endl;
    cout << "\t" << oct << x << "\t\t Octal" << endl;
    cout << "\t" << hex << x << "\t\t Hex" << endl;
    cout << "\t" << bitset<MAX_BITS>(x) << "\t Binary (LSB)" << endl;
    When you read & write a file in "binary", this does not mean base-2! It means "raw" or "unaltered", as contrasted with the "text" mode which formats text with carraige return and/or linefeed as required for text files on your system.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is it talking about using cin and cout like cin>>x, cout<<x so that it outputs in binary instead of ASCII text?
    No, it's talking about not making character set conversions of multiple bytes. When a stream is binary oriented, you'll get CR and LF separately instead of the two merged into '\n', for example.

    >Because my understanding is that write() and read() can do the job.
    Stream orientation isn't the same as I/O formatting. read and write are unformatted, it's largely irrelevant whether the stream is binary oriented or text oriented. The big difference is how certain text characters are treated differently as binary (the infamous CRLF duo translating to a single '\n', for example). That's why you can use read and write with a text oriented stream to no ill effect but have to be careful with the reverse. On the other hand, formatted input with operator<< or operator>> actually performs in memory conversions on the byte streams being worked with. So "0x1B" might be converted to its numeric representation, 27, so that it can be assigned to an integer.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    It depends on what you mean by "binary".
    I meant "raw" or "unaltered" data.

    Thanks for your strtol() example anyway, it was wonderfully helpful.

    No, it's talking about not making character set conversions of multiple bytes. When a stream is binary oriented, you'll get CR and LF separately instead of the two merged into '\n', for example.
    So interpreting "\r\n", '\n' - both of them as '\n', is that what it was talking about? Is there any other differences between binary I/O and text I/O except that?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is there any other differences between binary I/O and text I/O except that?
    No, that's pretty much it unless you want to talk about internationalization and multibyte characters.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM
  2. I'm REALLY confused. Why isn't cout or cin working here?
    By niudago in forum C++ Programming
    Replies: 8
    Last Post: 02-15-2003, 05:53 PM
  3. cin and cout
    By quentin in forum C++ Programming
    Replies: 6
    Last Post: 04-27-2002, 02:04 PM
  4. STL to: Cin, Cout, ifstream & ofstream
    By kuphryn in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2001, 09:32 AM
  5. i don't know about cin, cout, flag.
    By comwin in forum C++ Programming
    Replies: 2
    Last Post: 11-08-2001, 04:26 AM