streams again

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zeeshan708
    New Member
    • Aug 2009
    • 25

    streams again

    we say that iostream is a class template specialization for char i/o for C++ standard library class basic_iostream then why does it also does i/o other than char type.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    The char refers to the data type of the output (or input) stream. A stream is a long series of values and each value has a type. For a char stream each value has type char but (in theory) you can have a stream of any type.

    The classes convert from the stream to the type that you are interested in inputing/outputing.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You can use basic_iostream< char> if you want. iostream s just a typedef of this.

      You can also use basic_iostream< wchar_t>. This is typedef'd to wiostream.

      char and wchar_t are the only types you can use with basic_iostream.

      Comment

      • zeeshan708
        New Member
        • Aug 2009
        • 25

        #4
        ya but when we include iostream and do some output of int data type how come it works?

        Comment

        • mac11
          Contributor
          • Apr 2007
          • 256

          #5
          As Banfa said, the stream converts types for you.

          When you do, for instance:

          cout << 5;
          What prints is the text string "5", which is ascii or unicode or whatever to represent the character 5 as TEXT. It does not send the actual value 5 - if it did you would see some nonprintable character rather than a nice 5. The stream is smart and when you send it an int it converts it to text.

          If you want to dump raw binary representation of an int you can do that, but it's usually not what you want.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            a basic_iostream< int, etc...> requires a traits class. Int that traits class there is a char_type and an int_type. The char_type is the type used for the stream width and the int_type is an integer that can represent any valus of char_type plus the end-of-file or EOF character.

            Therefore, int_type is larger than char_type.

            If your char_type is int, then what is your int_type? Like, have you written a special traits class?

            Comment

            • zeeshan708
              New Member
              • Aug 2009
              • 25

              #7
              ok.thats good.now i get it.

              Comment

              Working...