You are reading the article C++ Basic Input/Output: Cout, Cin, Cerr Example updated in September 2023 on the website Nhahang12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 C++ Basic Input/Output: Cout, Cin, Cerr Example
What are Streams in C++?C++ provides users with a number of libraries that they can use to perform input/output tasks. These tasks are done in the form of byte sequences, popularly called streams.
The streams are divided into two:
Types of streams
Input Stream: This is a type of stream where the bytes flow from a device such as a keyboard to the main memory.
Output Stream: This is a type of stream where the bytes flow in the opposite direction, that is, from main memory then to the device such as display screen.
In this C++ tutorial you will learn:
How do streams work?C++ streams work as follows:
First, a stream is initialized with the right type.
Next, you should state where the I/O will occur using get/put pointers.
Function TableThe following are the functions provided in the stream.h header file:
Class Functions
Filebuf It sets file buffers to read/write. It has close() and open() functions in it
fstreambase It’s the base class for the classes ifstream, fstream, and ofstream. Its operations are common to the file streams.
ifstream It’s an input file stream class for providing input operations.
ofstream It’s an output file stream class for providing output operations.
fstream It’s an input/output stream class. It supports simultaneous input/output operations.
C++ Header files for Input/ OutputC++ provides three libraries that come with functions for performing basic input/out tasks. They include:
Iostream: It’s an acronym for standard input/output stream. This header file comes with definitions for objects like cin/ cout/cerr.
Iomanip: It’s an acronym for input/output manipulators. The library comes with functions that can be used for the manipulation of streams. It contains definitions for objects like setw, setprecision, and others.
Fstream: This is a header file for describing the file stream. It handles data that is read from file as input or that is written to a file, the output.
The cin and cout keywords are very popular in C++. They are used for taking inputs and printing outputs, respectively. To use them, you must include iostream header file in your program. The reason is that they are defined in that header file. Failure to include the iostream header file will generate an error. This results from a failure by the C++ compiler to understand the meaning of the keywords.
The major objects defined in the iostream header file are cin, cout, cerr, and clog. Let’s discuss them.
The cout object is an instance of the iostream class. It is used for producing output on a standard output device, which is normally the screen. It’s used together with the stream insertion operator (<<).
Example:using namespace std; int main() {
char welcome[] = “Welcome to Guru99”;
cout << welcome << endl;
return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
Include the iostream header file where the cout object is defined.
Include the std namespace so that we don’t have to call it when using its classes.
Call the main() function. The program code should be added within its body. The opening curly brace { marks the beginning of its body.
Create a character variable named welcome to hold the string Welcome to Guru99.
Print the value of the string welcome on the console. The endl is a C++ keyword meaning end line. It moves the cursor to begin printing text on the next line.
The program must return value upon successful execution.
End of the body of function main().
Example:The following example demonstrates how to use the cin keyword in C++:
using namespace std; int main() { int number;
cout << “Enter a number:”; cout << “nYou entered: ” << number;
return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
Include the iostream header file into our program. The cin object is defined in this header file.
Include the std namespace to use its classes. You will not have to call std when using its classes.
Call the main() function. The program code should be added within its body.
The start of the body of the program.
Declare an integer variable named number.
Print a message on the screen prompting the user to enter a number.
Read the value entered by the user on the console from the keyboard.
Print the value read above on the console alongside other text.
The program should return a value if it executes successfully.
End of the body of the main function.
The cerr object forms the standard error stream for outputting errors in C++. Cerr is an instance of the ostream class. The cerr object is unbuffered. This means it’s used when an error message is to be displayed immediately.
Since it’s unbuffered, it doesn’t store error message for a later display. It’s used together with the stream insertion operator (<<).
Example:using namespace std; int main() {
cerr << “An Error occurred!”;
return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
Include iostream header file where the cerr object has been defined.
Include the std namespace so that we don’t have to call it when using its classes.
Call the main() function. The program logic should be added within its body. The opening curly brace marks the beginning of the function’s body.
Use the cerr object to print an error on the console.
The program must return a value upon successful execution.
End of the body of the main function.
The clog object is an instance of the ostream class. It’s used to show errors on the standard display, the monitor. It’s similar to the cerr object, but it’s buffered. Since it’s buffered, it stores the error message in buffer till the buffer is filled/flushed. It’s used together with the stream insertion operator (<<).
Example:using namespace std; int main() { clog << “An Error occurred!”; return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
Including the iostream header file in which the clog object is defined.
Including the std namespace so that we can use its classes without calling it.
Calling the main() function. The program logic should be added within its body. The { marks the beginning of the function’s body.
Use the clog object to print an error on the standard output, the monitor.
The program must return a value upon successful completion.
The end of the body of function main().
Error handling with IO streams:To check whether a stream is valid or not, you can use it as a Boolean.
Here is an example:
ifstream file( "myfile.txt" ); if ( ! file ) { cout << "File NOT opened!" << endl; }To get more details for the stream status, you can use these functions:
good()- will return true if all is okay.
bad()- will return true if a fatal error occurs.
fail()- will return true after unsuccessful stream operation.
eof()- will return true if it reaches end of a file.
To know whether a particular read/write operation failed, test the read result.
For example, to check whether user entered a valid integer, do this:
int p; { cout << "Enter valid number" << endl; } Summary
The input and output tasks In C++ are done via byte sequence. The bytes are called streams.
In an input stream, the bytes flow from the input device like a keyboard to the main memory.
In an output stream, the bytes from the main memory then to an output device like a monitor.
The cout object is an instance of the ostream class. It produces output on output devices like the monitor.
The cerr object is an instance of the ostream class. It displays error objects on the monitor.
The clog object is instance of the ostream class. It displays error messages on output devices.
The clog object buffers error messages, whereas the cerr object does not.
You're reading C++ Basic Input/Output: Cout, Cin, Cerr Example
Update the detailed information about C++ Basic Input/Output: Cout, Cin, Cerr Example on the Nhahang12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!