Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Test whether iostream is binary or plain text

Test whether iostream is binary or plain text

Scheduled Pinned Locked Moved C / C++ / MFC
questiondesign
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Skippums
    wrote on last edited by
    #1

    I would like to test whether a stream was opened for binary read/writes or for plain text. The goal of my code is to output the data in a MyClass object in two different ways depending on this stream property.

    class MyClass {
    friend ostream& operator <<(ostream& stream, const MyClass& obj);
    friend istream& operator >>(istream& stream, MyClass& obj);
    }
    ostream& operator <<(ostream& stream, const MyClass& obj) {
    // How do I do the following?
    if ([stream is opened for binary writes]) {
    ...
    } else {
    ...
    }
    }
    istream& operator >>(istream& stream, MyClass& obj) {
    // How do I do the following?
    if ([stream is opened for binary reads]) {
    ...
    } else {
    ...
    }
    }

    Would doing such a thing be poor design, or is this pretty acceptable? Thanks,

    Sounds like somebody's got a case of the Mondays -Jeff

    R B 2 Replies Last reply
    0
    • S Skippums

      I would like to test whether a stream was opened for binary read/writes or for plain text. The goal of my code is to output the data in a MyClass object in two different ways depending on this stream property.

      class MyClass {
      friend ostream& operator <<(ostream& stream, const MyClass& obj);
      friend istream& operator >>(istream& stream, MyClass& obj);
      }
      ostream& operator <<(ostream& stream, const MyClass& obj) {
      // How do I do the following?
      if ([stream is opened for binary writes]) {
      ...
      } else {
      ...
      }
      }
      istream& operator >>(istream& stream, MyClass& obj) {
      // How do I do the following?
      if ([stream is opened for binary reads]) {
      ...
      } else {
      ...
      }
      }

      Would doing such a thing be poor design, or is this pretty acceptable? Thanks,

      Sounds like somebody's got a case of the Mondays -Jeff

      R Offline
      R Offline
      Rozis
      wrote on last edited by
      #2

      First: A stream is not binary or plain text, it is: do i want to treat incoming bytes as binary or as plain text. It is perfectly legal to open a stream containing plain text as binary. Next: you must open a stream with a flag that tells how you want to open it, so you already know if it's binary or not. Ergo: there's no need to test this. But i guess i'm completely missing the point...

      S 1 Reply Last reply
      0
      • R Rozis

        First: A stream is not binary or plain text, it is: do i want to treat incoming bytes as binary or as plain text. It is perfectly legal to open a stream containing plain text as binary. Next: you must open a stream with a flag that tells how you want to open it, so you already know if it's binary or not. Ergo: there's no need to test this. But i guess i'm completely missing the point...

        S Offline
        S Offline
        Skippums
        wrote on last edited by
        #3

        Ok, let me rephrase... one of the members of MyClass is an array. If the user wants to output my class in a human-readable form, I want to print the array as "[elem1, elem2, ... elemN]". If not, then I want to output the array length followed by each element in a binary representation. Now, when someone tries to output my class as "cout << MyClassInstance", it should be the human-readable version. When serializing the class to a file for later reloads, I want to output the binary version. How can I infer which version to use? I could conceivably write a specific implementation for ifstream and ofstream so I always read/write the non-human-readable version to a file, but that isn't really what I am trying to do. I could also write a specific method to serialize vs. just outputting my class, but again, I don't know what the standard way of doing this is. Thanks for any insight,

        Sounds like somebody's got a case of the Mondays -Jeff

        1 Reply Last reply
        0
        • S Skippums

          I would like to test whether a stream was opened for binary read/writes or for plain text. The goal of my code is to output the data in a MyClass object in two different ways depending on this stream property.

          class MyClass {
          friend ostream& operator <<(ostream& stream, const MyClass& obj);
          friend istream& operator >>(istream& stream, MyClass& obj);
          }
          ostream& operator <<(ostream& stream, const MyClass& obj) {
          // How do I do the following?
          if ([stream is opened for binary writes]) {
          ...
          } else {
          ...
          }
          }
          istream& operator >>(istream& stream, MyClass& obj) {
          // How do I do the following?
          if ([stream is opened for binary reads]) {
          ...
          } else {
          ...
          }
          }

          Would doing such a thing be poor design, or is this pretty acceptable? Thanks,

          Sounds like somebody's got a case of the Mondays -Jeff

          B Offline
          B Offline
          BonshatS
          wrote on last edited by
          #4

          std::ios_base::open_mode can tell you how the file stream was opened. The values are listed in xiosbase.h [edit] in VS.

          modified on Monday, February 1, 2010 8:42 AM

          S 1 Reply Last reply
          0
          • B BonshatS

            std::ios_base::open_mode can tell you how the file stream was opened. The values are listed in xiosbase.h [edit] in VS.

            modified on Monday, February 1, 2010 8:42 AM

            S Offline
            S Offline
            Skippums
            wrote on last edited by
            #5

            After reading your post, I assumed that I could just use the statement:

            bool const isBinary = ((out.flags() & ios_base::binary) != 0);

            However, after testing this it appears that the ios_base::open_mode is not stored in the return value of "ostream::flags()". I have been unable to figure out how to get something from an ostream object that tells me the ios_base::open_mode used. Any idea how to extract this information after the file has been opened? Thanks,

            Sounds like somebody's got a case of the Mondays -Jeff

            B 1 Reply Last reply
            0
            • S Skippums

              After reading your post, I assumed that I could just use the statement:

              bool const isBinary = ((out.flags() & ios_base::binary) != 0);

              However, after testing this it appears that the ios_base::open_mode is not stored in the return value of "ostream::flags()". I have been unable to figure out how to get something from an ostream object that tells me the ios_base::open_mode used. Any idea how to extract this information after the file has been opened? Thanks,

              Sounds like somebody's got a case of the Mondays -Jeff

              B Offline
              B Offline
              BonshatS
              wrote on last edited by
              #6

              Wow. 9 months later. Internet been a bit slow? I'm sorry, I also incorrectly assumed the mode would be accessible somewhere within the stream obj. Other than scanning the array data for CR,LF combos, or trying different read/write modes and seeing what fails and what succeeds, I don't how to determine how the stream was opened.

              S 1 Reply Last reply
              0
              • B BonshatS

                Wow. 9 months later. Internet been a bit slow? I'm sorry, I also incorrectly assumed the mode would be accessible somewhere within the stream obj. Other than scanning the array data for CR,LF combos, or trying different read/write modes and seeing what fails and what succeeds, I don't how to determine how the stream was opened.

                S Offline
                S Offline
                Skippums
                wrote on last edited by
                #7

                Yeah, clearly I get sidetracked easily. Anyway, I ultimately determined that opening a file in binary mode doesn't really do what I thought it did... I was under the impression that doing so would force the shift operator to output data in binary, which it doesn't. Therefore, I decided to implement two methods: one to output the data in binary, and the other to output it in ASCII format. Thanks for the help, though.

                Sounds like somebody's got a case of the Mondays -Jeff

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups