Blog
27 BEST eCommerce Platforms in 2021
An eCommerce platform is a software application that helps online businesses to manage their...
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:
In this C++ tutorial you will learn:
C++ streams work as follows:
The 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++ provides three libraries that come with functions for performing basic input/out tasks. They include:
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 (<<).
#include <iostream> 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:
The cin object is an instance of the istream class. It reads input from the input device, the keyboard. It is normally used together with the extraction operator (>>). The extraction object is responsible for extracting data entered through the keyboard from the cin object.
The following example demonstrates how to use the cin keyword in C++:
#include <iostream> using namespace std; int main() { int number; cout << "Enter a number:"; cin >> number; cout << "\nYou entered: " << number; return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
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 (<<).
#include <iostream> using namespace std; int main() { cerr << "An Error occurred!"; return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
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 (<<).
#include <iostream> using namespace std; int main() { clog << "An Error occurred!"; return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
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:
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; if ( cin >> p ) { cout << "Enter valid number" << endl; }
An eCommerce platform is a software application that helps online businesses to manage their...
What is TCL? TCL is shell application that reads TCL command from its standard input or from a...
What is the URL? A URL is a global address of documents and protocols to retrieve resource on a...
What is Software Engineering? Software engineering is a process of analysing user requirements and then...
A download manager is a software that helps you to prioritize your downloads, faster download...
This tutorial aims at introducing the apply() function collection. The apply() function is the...