answersLogoWhite

0


Best Answer

A stream class is a class that encapsulates a character buffer and allows simple insertion or extraction of character sequences from or to the buffer. A character sequence may be a single character or a string of characters. The stream may be associated with a device such as a keyboard (for extraction), a display (for insertion), a storage device such as a disk file (for either extraction or insertion, or both), or some other device that can process character sequences. The stringstream class has no device associated with it; it merely provides a character buffer for insertion or extraction from memory.

Any object that can be converted to a character sequence can be inserted into an output stream object, either by using the object's insertion operator (<<) or via one of the put() member methods associated with the stream. The standard library's output stream insertion operators provide overloads for all the built-in data types; char, int, float, double and pointer types, including all their modified types (signed, unsigned, long and short) as well as their aliases such as size_t. All the standard library <string> types are supported by all standard library streams, as are C-style strings. The insertion operator can also be overloaded to cater for all user-defined types that require it. C-style arrays and STL containers are not supported, however you can manually iterate through a sequence and insert the individual elements in an output stream, provided those element types have an appropriate insertion operator overload associated with them.

Character sequences can be extracted from an input stream using the stream's extraction operator (>>). As with output streams, the extraction operator is overloaded to cater for all built-in types, C-style strings and <string> types and can be overloaded to cater for user-defined types. When extracting a character sequence, all whitespace characters are ignored but are used to delimit each individual sequence. The stream's get() methods can be used to extract strings of arbitrary length (including whitespace) as well as use delimiters other than whitespace.

Never assume that a numeric value extracted from a stream can be converted to a valid value. Always extract to a string first, then perform the conversion to a numeric value. A common technique is to extract a string from the stream, then insert the string into a temporary stringstream object and finally extract from the stringstream to the numeric type. If the final extraction fails then the input string was not numeric. The stringstream will evaluate false (its fail bit will be set) but the original stream remains valid because extracting a string (even an empty string) is always a valid operation.

Both input and output streams use bit flags to determine their state. For instance, if you attempt to extract beyond the end of a stream associated with an input file, the eof bit is set. Note that reading the final sequence does not set the bit -- it is only set when you attempt to read another (non-existent) sequence. When a stream is used in a boolean expression, these flags are used to determine how the object is evaluated. If no flags are set, the object evaluates true, meaning the object is still in a valid state. If the return value is false, you can test individual flags to determine the state.

The following code snippet demonstrates both insertion and extraction from the standard I/O streams plus how to test for the successful extraction of an integer from the standard input stream (typically the keyboard) using a temporary stringstream:

int enter_integer (const std::string& prompt)

{

int i;

for (;;) // Forever loop -- repeat until input is successful.

{

// Temporary variables (on each iteration, these variables are created anew).

std::string input;

std::stringstream ss;

// Insert the given prompt to the standard output device.

std::cout << prompt;

// Extract input from the standard input device.

if (!(std::cin >> input))

{

// Input shouldn't fail, but if it does there's nothing we can do about it.

// Let the caller deal with it. See note below.

throw std::exception ("Unable to read from std::cin");

}

// Insert the temporary input string into the temporary stringstream object.

ss << input;

// Attempt extraction to an integer (returns true if successful)

if (ss >> i)

{

break; // Extraction successful, exit the loop.

}

// Extraction failed.

std::cerr << "Invalid input. Please re-enter.\n";

// The temporaries will fall from scope at this point,

// taking the bad input with them. The input stream

// remains valid for the next iteration.

}

return i;

}

Note: throwing an exception is never a good thing, but in this case it is the best option. If the exception is thrown, it means there's something seriously wrong with std::cin. Since that could affect the entire program, the best place to deal with this is in the program's main function, which should include a catch-all exception handler to handle any and all unhandled exceptions.

int main()

{

int num;

try

{

num = enter_integer ("Enter an integer: ");

}

catch (std::exception& e)

{

// Perform any necessary cleanup and log the error.

std::cerr << e.what() << std::endl;

return -1;

}

catch (...)

{

// Perform any necessary cleanup and log the error.

std::cerr << "An unknown exception occurred." << std::endl;

return -1;

}

// use num...

return 0;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are C Plus Plus stream classes?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are filestream classes in c plus plus?

The file stream classes (ifstream and ofstream) are derivatives of the I/O stream classes (istream and ostream) that are specific to file input and output.


Which stream functions in c plus plus control the formatting of input and output values of a class?

None of them. To control the formatting of your classes, you must overload the stream insertion and extraction operators.


How is file handling applied in c plus plus?

File handling is handled by input stream objects (ifstream) and output stream objects (ofstream), or bi-directional streams (fstream). These classes are derived from istream and ostream. See related links for more information on these classes.


Drawbacks of c plus plus?

Not as commonly used. More schools are replacing their c++ classes with java classes.


Are all C plus plus classes subtypes?

no


Why do you need c plus plus?

For programming. C++ is better than C because it is object-oriented and has classes.


What year c plus plus was developed?

Developed in 1979 by the name of C with classes. Renamed to C++ in 1983.


What is diffence between c and c plus plus?

main difference b/w c and c++ is that c is procedural language whereas c++ is object oriented language also classes are not used in c but in c++ classes are used.


What was c plus plus previously called?

'C with Classes' began development in 1979. The name changed to 'C++' in 1983.


Programming codes under classes in c plus plus?

Are called methods.


How you use stack in c plus plus classes?

It would be easier to manipulate the stack in assembly language rather than C++.


What are logical classes in c plus plus?

There is no such thing. Logic is bitwise operation, not a data type.