Friday, June 26, 2015

File Handling in C++



Hey, what's up guys??

Today we will look in to File Handling in C++.

Before read or write to files we must know what are the classes or the header files that we must include to our program.
There are three classes:

  • ofstream = Class to write in files
  • ifstream = Class to read from files
  • fstream  = Class to both read and write from and to files
Including the header file to the program can be done as follows:
#include<fstream>

After including the header file we can use all the methods declared in that class. Since C++ is object oriented we must have to create a object from one of the above classes to use the functions.

At first we are using the class to read a file, so its better to create the object from the ofstream class.

ofstream MyObj; //Created a object named MyObj

After creating the object we must first use the open() function to open the file for reading.(or writing)
This function accepts two parameters. First on is the file name and second one is the mode that we want to open the file. Second parameter is optional.

MyObj.open("myTextFile.txt"); //opened a text file named myTextFile.txt in the readable form

After opening the file in the readable format lets write something to the file. First lets define a string variable named myContent. And let's read from the file.

String myContent;
MyObj>>myContent; //Reading the whole file content  and storing in a single string variable

Or else we can read the file line by line.

while(getline(MyObj,myContent)){
    cout<<myContent<<endl;  //this will print the content in the file line by line to the screen till the end of the line reaches
}


Well there, we read the content from a file. Let's try to write in to a file in C++.

To write to a file, first we must open the file in writable mode. We can do it by creating an object from the ofstream and opening the file using open() method.

ofstream myObj2; //created an object using ofstream class
myObj2.open("myTextFile.txt"); //opened a text file named myTextFile.txt in the writable mode




After opening the file in writable form we can simply pass values or variables and write values and content to the file.

myObj2<<"This line will be added to the text file"<<endl;

myObj2<<myContent<<endl;

To check whether the file is still open in the program we can use is_open() method. This method does not accept any arguments and will return a boolean value.

if(myObj2.is_open()){
    //body of the if condition
}


After we read and write and using the file we must close the file from our program. Otherwise it will hold some memory space and will make the program inefficient and affect the system resources badly. To close the filr from the program we can use close() function in C++.

myObj2.close();

These are the basic functions used in file handling in C++.



Past Topics:-
Like Us on Facebook :-



Comment box is open for your comments, ideas, suggestions and complaints!

No comments:

Post a Comment