Input/Output in C++



Input/output operations are the operations through which a program receives data or displays results. These operations should be viewed from the program's perspective


Practically, the data that enters the program or leaves the program are strings of characters that the program receives or sends

The C++ language offers a uniform way to perform input/output operations, whether they are done on the console, in files, or with other devices that process characters. This is called a stream. A stream can be viewed as a sequence of characters that are sent in a well-determined order from a source to a destination. The program will insert characters into the stream (if it is an output stream, which displays data) or will extract characters from the stream (if it is an input stream, from which data is read). Next, we will talk about cout and cin – the standard output and input streams.


The cout Output Stream

In most cases, the standard output device is the screen and can be accessed with the cout stream. For this, cout is used together with the insertion operator "<<" followed by the data to be displayed:

cout << "Hello"; // displays Hello on the screen
cout << 17; // displays the number 17 on the screen
cout << n; // displays the value of the variable n on the screen

The cin Input Stream

In most cases, the standard input device is the keyboard and can be accessed with the cin stream. For this, cin is used together with the extraction operator ">>", followed by the variable in which the extracted value will be stored (the variable to be read):

int n;
cin >> n;

First, the variable n is declared, then a value is read for it – a value is extracted from cin and stored in the variable n. When running, the program waits for a value to be entered from the keyboard. In fact, the characters entered are transmitted to the program only when the ENTER key is pressed.



Input/Output Operations with Files in C++


Standard input/output operations are done with the keyboard and screen, but it is also possible to perform readings from text files and writings to text files. To perform the actual operations, the files are associated with data streams, and the operations are similar to those with the keyboard and screen.


Steps for Working with Text Files

In C++ there are several ways to work with text files. They all follow these steps:
  1. opening the file/associating the file with a data stream;
  2. reading from the file/writing to the file;
  3. closing the file/data stream.

Opening the File

A common way to open files is to declare variables of stream type. These are of type:

The declaration of variables can be done as follows:
ifstream fin("INPUT_FILE_NAME");
ofstream fout("OUTPUT_FILE_NAME");

Reading from the File/Writing to the File

To actually read data from the file/write data to the file, the extraction from stream/insertion into stream operators are used.

For example:
int x;
fin >> x;
fout << 2 * x;

Closing the Files


It is done as follows:
fin.close();
fout.close();

Color Contrast

Text Size

Text Spacing

Reading Aids


In this section you can generate a summary of the page content using AI! Feel free to use the button below whenever you are in a hurry and don't have time to learn everything!


Summary

In this section you can ask our expert robot anything related to the questions you encountered during the lessons! Feel free to use the button below whenever you need additional explanations!


Chatbot