File operations

File operations in C++ | c++ Advanced | Part – 12

C++ allows us to perform operations on files.

We can read contents of a file and even write some content in the file through a C++ program.

Isn’t that awesome?

To perform operations on files, C++ provides several built-in functions.

These functions are all declared in fstream header file.

To perform operations on file, the fstream and iostream header files should be included in the program.

Opening a file

NO, you won’t need any unlocking charm to open the files.

To open a file, open() function is used

open() opens an existing file.

If the file is not found it creates a new file.

The open() function takes in two parameters – name of the file and mode of opening.

The mode of opening can be any of the following:

in – read mode.

out – write mode.

app – append mode. It searches for the last character in the file and starts writing from there.

trunc – If the file exists its content will be truncated before opening.

Let’s see how open() is written

 open(“file_name”, ios::mode);

So, if we want to open a file named programming.txt in read mode. We will write

 fstream newFile;

newFile.open(“programming.txt”, ios::in);

Where newFile is an object of type fstream.

Reading a file

To read a file we use the >> operator.

But instead of cin, we use the fstream object.

 fstream newFile;

newFile>>whatever;

Where whatever is the variable in which the whole read data will be stored.

So, if we want to read a file named programming.txt. We will write

 fstream newFile;

char data[500];

newFile.open(“programming.txt”, ios:in);

newFile>>data;

Thus, we have successfully read the file programming.txt.

Writing a file

To write in a file we use the << operator.

But instead of cin, we use the fstream object.

 fstream newFile;

newFile<<whatever;

Where whatever is a variable which contains the data to be written in the file.

So, if we want to write some text in a file named programming.txt. We will write

 fstream newFile;

char data[500] = “Hello World”

newFile.open(“programming.txt”, ios:out);

newFile<<data;

That’s it. “Hello World” will be written to the file.

Closing a file

Now, we know how to open, read and write a file.

After all these operations are successfully carried out, we need to close the file.

To do so, fstream provides us close() function.

To close a file we write

 fstream newFile;

newFile.close();

So, to close a file named programming.txt. We will write

 fstream newFile;

newFile.open(“programming.txt”);

newFile.close();

It will close the file programming.txt.

To Summarize

  • open() is used to open an existing file and if the file is not found it creates a new file.
  • Inoutapptrunc are various file open modes.
  • >> is used to read data from a file.
  • << is used to write data to the file.
  • close() is used to close a file.

Leave a Comment

Your email address will not be published. Required fields are marked *

PYQ of History UGC NET UGC NET Mathematical Reasoning and Aptitude ICT (Digital Initiatives) | UGC NET | paper – 1 The Scope of Information and Communication Technology(ICT) PYQ of People, Development, and Environment Top 30 PYQ of HINDI | UGC NET – 2023 Top 30 PYQ of Teaching Aptitude PYQ of Research Aptitude | NTA UGC NET | Paper 1 | Part 1 UGC NET Paper 1 Syllabus | Updated | 2023 Types of Research | Research Aptitude | nta ugc net | UGC NET 2023