-3

Java has byte stream classes (Stream and its subclasses), and character stream classes (Reader and Writer and their subclasses).

Does C++ have both byte stream classes and character stream classes?

Does http://www.cs.sjsu.edu/~pearce/modules/lectures/oop/streams/streams.htm

The standard C++ Stream Library provides input stream, output stream, and input-output stream classes. These are all character-oriented streams.

mean that C++ has only character stream classes but no byte stream classes?

halfer
  • 19,824
  • 17
  • 99
  • 186
Tim
  • 1
  • 141
  • 372
  • 590
  • 1
    What is the diefference between a byte and a character stream? I feel like in C++ you choose between them by picking an open mode (i.e. binary for byte stream, character without) – Borgleader May 18 '19 at 00:57
  • Thanks. (1) the difference between a byte and a character stream is also explained in the link. For example: Java has byte stream classes (Stream and its subclasses), and character stream classes (Reader and Writer and their subclasses). (2) Can you elaborate " in C++ you choose between them by picking an open mode (i.e. binary for byte stream, character without)"? – Tim May 18 '19 at 00:59
  • 3
    You just repeated the information in the question, what does the Byte stream do differently versus the character stream? – Borgleader May 18 '19 at 01:01

1 Answers1

2

Remember that in C++, the canonical way to represent a byte is char, which is an integral type. 1

The C++ standard provides the Input/Output Library, which provides basic input and output streams. These basic streams are templates, and the 'character' type that they work on is chosen with a template parameter. Typically, these are used in the char and "wide character" wchar_t flavours. Other character types exist, like char8_t and char32_t, but their usage is a bit uncommon.

For example, file input and output in C++ can be done with a basic_fstream object, which takes as template parameter some type CharT. For convenience, we have two aliases: fstream is shorthand for basic_fstream<char>, and wfstream is shorthand for basic_fstream<wchar_t>. This pattern is also seen across other streams, like stringstreams and the standard input and output streams.

At a glance, it might seem like the "wide character" wchar_t would be great for working with Unicode strings. Don't be fooled. Unicode support in C++ is seriously lacking.

1. Admittedly, there is now a standard std::byte, but it's usage is fairly limited. It's neither an integral type nor a character type.

Note: Don't mix cout and wcout in the same program. Here's why.

alter_igel
  • 6,899
  • 3
  • 21
  • 40
  • 1
    If by “white space may be skipped implicitly” you’re referring to treating white space as a field separator, that’s got nothing to do with text mode versus binary mode. It’s about formatted and unformatted IO operations. Stream extractors (`<<`j and inserters (`>>`), among others, do formatted input. `read` and `write`, among others, do unformatted. Formatted IO operations skip leading white space and treat the next white space as the end of the input field. – Pete Becker May 18 '19 at 02:43
  • @PeteBecker thanks for the feedback; I should have done my research more carefully – alter_igel May 18 '19 at 19:43