Input / Output in C ++
The C ++ standard library provides many possibilities for input / output and will be discussed in later chapters. In this chapter, we discuss the most basic and popular I / O operations required for C ++ programming.
I / O in C ++ takes place in Streams, which are sequences of bytes. If bytes flow from a device, such as a keyboard, disk drive, or a network connection . to the main memory, it is called input operation. If the bytes flow from main memory to devices, such as display screens, printers, dist drives, or a network connection . it is called output operation.
File header for I / O in C ++
The following table lists important file headers for C ++ programs:
Header File This file description defines cin, cout, cerr and clog objects, corresponding to Standard Input Stream, Standard Output Stream, Un-buffered Standard Error Stream (Stream). Non-buffered standard errors) and Buffered Standard Error Stream (Buffer Standard Error Stream). This file declares useful services to perform formatted I / O operations with parameterized thread operators such as setw and setprecision . This file declares the user-controlled file processing services. We will discuss it in detail in the File and Stream chapters in C ++Standard Output Stream (cout) in C ++
The pre-defined cout object is an illustration of the ostream class. The cout object is referred to as "connected to" standard output device, usually the screen. The cout object is used in conjunction with the insertion operator, written as <<, as the example below:
#include using namespace std ; int main ( ) { char str [] = "Xin chao C++" ; cout << "Gia tri cua str la: " << str << endl ; }
When the above code is compiled and executed, it gives the following result:
Gia tri cua str la : Xin chao C ++
The C ++ compiler also determines the data type of the variable to be output and selects the appropriate insert operator to display the value. The << operator overloads to the output data of type integer, float, double, string, and pointer values are available.
Thread insertion operator << can be used more than once in a command and endl is used to add a new line at the end of that line.
Standard Input Stream (cin) in C ++
The pre-defined object cin is an illustration of the isrtream class. The cin object is considered to be attached to a standard input device, which is usually a keyboard. Cin objects are used in combination with extraction operator, written as >>, as in the following example:
#include using namespace std ; int main ( ) { char ten [ 50 ]; cout << "Nhap ten cua ban (viet lien): " ; cin >> ten ; cout << "Ten ban la: " << ten << endl ; }
When the above code is compiled and executed, it will prompt you to enter a name. You enter a value and then enter to see the result like this:
The C ++ compiler also determines the data type of the entered value and selects the appropriate flow operator to extract the value and store it in the provided variables.
The thread extraction operator >> can be used more than once in a command. To request more than one standard data, you can use:
cin >> ten >> tuoi ;
It is equivalent to the following two commands:
cin >> ten ; cin >> tuoi ;
Standard Error Stream (cerr) in C ++
The pre-defined object cerr is an illustration of the ostream class. The cerr object is considered to be attached to the standard error device, which is also the display but the cerr object is Un-buffered and each insert to cerr makes its output appear immediately .
The cerr object in C ++ is also used in conjunction with the thread insertion operator, as in the following example:
#include using namespace std ; int main ( ) { char str [] = "Khong the doc ." ; cerr << "Thong bao loi la: " << str << endl ; }
When the above code is compiled and executed, it gives the following result:
Thong bao loi la : Khong the doc .
Standard Log Stream (clog) in C ++
The pre-defined object clog is an illustration of the ostream class. The clog object is seen as attached to the standard error device, which is also the display but the clog object is padded. That is, each thread insertion to the clog as its output is kept in a buffer until the buffer is filled or until the buffer is flushed (transferred from disk buffer).
The clog object in C ++ is also used in conjunction with the thread insertion operator, as in the following example:
#include using namespace std ; int main ( ) { char str [] = "Khong the doc ." ; clog << "Thong bao loi la: " << str << endl ; }
When the above code is compiled and executed, it gives the following result:
Thong bao loi la : Khong the doc .
You won't be able to see any difference between cout, cerr and clog with these small examples, but while writing and executing large programs, this difference becomes more obvious. Therefore, it is a good practice for you to display error messages using the cerr stream and while displaying other log messages, the clog should be used.
According to Tutorialspoint
Last lesson: Date and Time in C ++
Next lesson: Struct in C / C ++
You should read it
- Input & Output in C
- Function setbuf () in C
- Read / write File in C ++
- Navigation IO in Unix / Linux
- Load the Input / Output operator stack in C ++
- How to Use I/O Streams in Java
- Read - Write File in C
- Function vfprintf () in C
- stdio.h in C
- Function fprintf () in C
- Update the BIOS for the PC motherboard in 5 steps
- Instructions for entering BIOS on different computers
Maybe you are interested
Download standard and beautiful Word file experience initiative cover template
How many ticks are in 1 second? Standard conversion method
1 degree C equals how many degrees F, degrees K? standard conversion formula
Download link for Adobe After Effects 32bit, 64bit FULL standard free
How to make standard Youtube videos?
Summary of Warcraft 3 codes, the most standard Warcraft 3 money orders