File Handling in C++ using Class

File Handling in C++ using Class

You should know about data persistence and life of data inside a program before knowing about file handling.

What is Data Persistence?

1. Data persistence refers to the existence of data in program.

2. Life of data is generally referred as data persistence.

3. All data (like global variable, local variables, etc) lost when C++ programs end.

4. Its memory is reclaimed by operating system, it then erased and handed back to other programs.


What are Streams?

1. Streams in C++ referred to the series of characters that are to be transferred between the programs thread and input or output.

2. Stream shows the flow of data into the program or out of the program.

3. Stream provides connection between programs and files so that we can write into the files and read from the files.

3. It is an abstraction for the user i.e., user need not to worry about how data are entered into the variable using cin and how data are shown on the screen using cout.


What are Input and Output Streams?

1. If the direction of flow of bytes is from file to variable in the program, it is called input stream.

2. If the direction of flow of bytes is from variable to file in the memory, it is called output stream.


streams-in-cpp-learning-mania



In the above figure, input stream and output stream are shown.


ios Stream Classification in C++

ios-streams-classification-in-cpp-learning-mania


File Handling in C++ using Class

1. Files are dealt using 3 classes namely ifstream, ofstream and fstream in C++.

2. ifstream and ofstream are available in fstream header file.

3. ofstream is that stream class which is used for writing in files.

4. ifstream is that stream class which is used for reading from files.

5. fstream is that stream class which is used for both reading from files and writing in files.

6. Firstly you need to open any file for reading or writing operations on file.


How to Open Files for reading or writing operations?

You can open any files in C++ using open method as shown below:

ifstream fin;

fin.open(filename, openmode);
fin.open(“filename”);

How to Close Files after performing operations?

It is a good habit to close the file after performing operations inside it. You can close it using close method as shown below:

ifstream fin;

fin.open(filename, openmode);
fin.open(“filename”);

//... after performing operations ...

fin.close();

How to write in a file?

#include<iostream>
#include<fstream>
using namespace std;

int main() {
 ofstream fout;
 fout.open("myfiles.dat");
 fout << "Hello Students!";
 fout.close();
}

How to read content of a file?

#include<iostream>
#include<fstream>
using namespace std;

int main() {
 ifstream fin;
 char ch;
 
 fin.open("myfiles.dat");
 fin >> ch;    //or ch = fin.get();
 
 //eof means end of file. 
 while(!fin.eof()) {
  cout << ch;
  fin >> ch;
 }
 
 fin.close();
}

File Opening Modes

1. ios :: in   >>   open in input/read mode.
2. ios :: out   >>   open in output/write mode.
3. ios :: app   >>   open in append mode.
4. ios :: ate   >>   open in update mode.
5. ios :: binary   >>   open in binary mode.

Text Mode vs Binary Mode

Text mode is the default file opening mode.
Binary mode can be specified with ios :: binary.


<< Previous                                                                                      Next >>
Previous
Next Post »
1 Response to "File Handling in C++ using Class"