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:

Input / Output in C ++ Picture 1

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 ++

4 ★ | 1 Vote

May be interested

  • Read - Write File in CRead - Write File in C
    the previous chapter explained the standard input and output devices handled by the c language. in this chapter we will see how programmers create, open and close text files or binary files with data. storage.
  • Store the output of a Linux command to a fileStore the output of a Linux command to a file
    usually, while working on a linux terminal, you may want to save the output of a command to a file. here are 4 different ways in which to save terminal content in a file.
  • How to Change Your Input Method in Max OS X LionHow to Change Your Input Method in Max OS X Lion
    depending on where you live, the placement of keys on your keyboard will differ. this can be confusing if you're used to using a certain keyboard to type in a certain language. for this reason, you can customize the input your mac receives...
  • How to fix 'Input Signal Out of Range' error on WindowsHow to fix 'Input Signal Out of Range' error on Windows
    when connecting an external monitor to a windows pc, you may encounter an input signal out of range error. this error usually occurs if you have a high refresh rate monitor connected to a lower end graphics device.
  • Instructions on how to create input forms in Excel extremely fast and simpleInstructions on how to create input forms in Excel extremely fast and simple
    creating an input form in excel is a very necessary tip if you are an accountant or office worker. it helps you to make statistics and input data for excel simpler and faster. below, tipsmake will guide you the most basic steps to create a simple input form.
  • How to Reflash Your BIOSHow to Reflash Your BIOS
    bios is short for basic input-output system. it is a set of electronic instructions stored on a chip inside your desktop or laptop. these instructions tell the computer how to perform the post (power on self test) and allow rudimentary...
  • What connection ports do projectors usually have?What connection ports do projectors usually have?
    projectors have connections for devices ranging from laptops to game consoles. this can make it difficult to find the right input or output. let's learn about the types of connectors for projectors through the following article!
  • How to limit data with Data Validation in Google SheetsHow to limit data with Data Validation in Google Sheets
    the data validation function is used to prevent users from entering anything other than correctly formatted data within a specific range. and this is how to use it.
  • 5 tips for using the BIOS to help you master your computer5 tips for using the BIOS to help you master your computer
    most computer users often don't care much about the bios. however, when the problem occurs, you need to tweak a setting and don't know how. you will wonder what the bios is? really need to know about it?
  • Input element attributes in HTMLInput element attributes in HTML
    the article below introduces the properties of input elements when creating forms in html.