What are the different classes used for reading and writing to and from files in C++?

Different classes used for reading and writing to and from files are as follows:

Classes Description
ofStream This class is used for writing onto files
ifStream It is used for reading from files
fStream It is used for both reading and writing onto files

What are the different file opening modes in C++?

Different file opening modes in C++ are as follows:

Modes Description
ios::in Opens a file for reading. New contents cannot be written in the file.
ios::out Opens a file for writing. If file exist already then its contents are deleted but if the file does not exist it will create a new file.
ios::binary It opens a file in binary form.
ios::truncate Opens a file for writing. If file already exists then its previous contents are replaced with the new contents.
ios::app Opens a file in append mode. It adds new content at the end of the file.
ios::ate It is used to write new contents at the end of the file and we can also modify the previous contents.
ios::nocreate It does not allow to create a new file if it does not exist.
Ios::noreplace It does replace the old file with new file.

How to open and close files in C++?

A header file fstream.h is used in order to work with the files in C++ which include functions for opening and closing files.
For opening a file:
In order to open a file open() member function of fstream’s class is used.
Syntax: open (filename, mode)
Here filename is the name of the file to open and mode can be mode available in C++.
For closing a file:
In order to close a file close() member function of fstream’s class is used. This function takes no arguments.

What are escape sequences?

Escape sequences are used with a ‘\’ backslash character and are used in printf statements in order to display the output of the program in a specific form. List of escape sequences:

Character Escape sequence

Double quotation mark \”
Single quotation mark \’
Question mark \?
Backslash \\
Beep \a
Backspace \b
Formfeed \f
Newlinw \n
Return \r
Tab \t