C++ - SPLessons

File Handling in C++

Home > Lesson > Chapter 34
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

File Handling in C++

File Handling in C++

shape Introduction

File Handling in CPP chapter gives the clear description about the functions performed on a file. Till now, standard library iostream was used for reading and writing into a program with the help of cin and cout. Reading and writing into file can be done using fstream standard library. fstream must be used as header file while doing input and output operations to a file along with iostream.

Open a file

shape Description

In order to write or read into a file, the file should be opened first. To open a file, C++ uses open() function.

shape Syntax

void open(const char* file_name,ios::openmode mode);
  • Here, first argument of open function defines the name of file and the address of the file.
  • Second argument represents the mode in which the file has to be opened. The following modes are used.
One can check whether a file is opened or not by using is_open() function. If the file is opened the compiler returns true otherwise false.

shape Modes

Modes Description
in Opens the file to read(default for ifstream)
out Opens the file to write(default for ofstream)
binary Opens the file in binary mode
app Opens the file and appends all the outputs at the end
ate Opens the file and moves the control to the end of the file
trunc Removes the data in the existing file
nocreate Opens the file only if it already exists
noreplace Opens the file only if it does not already exists

shape Example

One can combine the different modes using or symbol |. Eg:
ofstream  splessonsfile; splessonsfile.open("file.spl", ios::out | ios::app );
Here, input mode and append mode are combined which represents the file is opened for writing an appending the outputs at the end.

Close a file

shape Description

As soon as the program terminates, the memory is erased and frees up the memory allocated and closes the files which are opened. But it is better to use close() function to close the opened files after the use of file.
void close();

Writing and Reading to a file

shape Description

The information can be written to a file by using stream insertion operator << and read from a file using stream extraction operator>>.

shape Example

[c] #include <iostream> #include <fstream> using namespace std; int main () { char info[50]; //open a file to write ofstream ofile; ofile.open("mfile.dat"); cout << "Writing to a file" << endl; cout << "What is your name? "; cin.getline(info, 50); //writing entered data into the file. ofile << info << endl; cout << "What is your age? "; cin >> info; cin.ignore(); //write again inputted data into the file. ofile << info << endl; //closing the opened file. ofile.close(); //open a file to read ifstream ifile; ifile.open("nfile.dat"); //Reading from file and displaying cout << "Reading from the file" << endl; ifile >> info; cout << info << endl; //closing the opened file. ifile.close(); return 0; }[/c] Output [c] Writing to a file What is your name? splessons What is your age? 10 Reading from the file 10[/c]

Summary

shape Key Points

File Handling in CPP : Opening, closing, writing, reading, deleting and renaming are the functions used for File Handling in CPP.

shape Programming Tips

It is better practice to delete the files if not necessary to save the memory.