File Handling in C++ || C++

File Handling in C++:

File Handling means storing various data in a file with the help of programming Language. In C++ programming language, file handling is the concept which helps us to create, manipulate and play with the files.we can also extract/fetch data from a file to work with it in the program.

In C++ for file handling we need to use a header file called as " fstream " which enable us to use some special data types like : 

1) ofstream :- It is known as output file stream, used to create files and write stuffs into the files.

2) ifstream :- It is known as input file stream, used to read information from files. 

3) fstream :- It is known as the file stream, this can perform the functions of both ifstream and ofstream i.e. it can create, write and even read the files.


Now we will have a look on the functions with the help of which we can open and close files using the above data-types :-


open() :- This function is used to open or create a file. 

read() :- This function is used to read a file. 

write() :- This function is used to write information inside a file. 

close() :- This function is used to close a file.

 

A file in C++ can be opened in various modes like read-only mode or write mode and etc. So let's have a look on various modes of opening a file :- 


ios::in :- In this mode ios means input output stream and this mode opens a file in read-only mode.

ios::out :- In this mode ios means input output stream and this mode opens a file in write mode.

ios::binary :- In this mode ios means input output stream and this mode opens a file in binary mode.

ios::app :- In this mode ios means input output stream and this mode opens a file in write mode where all the output will get appended at the end of the file, basically app defines the appending nature of this mode.

ios::trunc :- In this mode ios means input output stream and this mode is used to remove some data from the files.

ios::nocreate :- In this mode ios means input output stream and this mode opens a file only if it exists already and if it's not then it won't create and open.

ios::noreplace :- In this mode ios means input output stream and this mode opens only if does not exists already that means it will not open the file that exists already rather it will create only new files.


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post