0% found this document useful (0 votes)
84 views4 pages

File Modes - Unit 5

Data structures file modes refrences notes

Uploaded by

wilfredjustin4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views4 pages

File Modes - Unit 5

Data structures file modes refrences notes

Uploaded by

wilfredjustin4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

FILE MODES

In C++ we have a set of file handling methods. These include ifstream,


ofstream, and fstream. These classes are derived from fstrembase and from the
corresponding iostream class. These classes, designed to manage the disk files,
are declared in fstream and therefore we must include fstream and therefore we
must include this file in any program that uses files.

File modes determine how a file is opened—whether it is for reading, writing,


or appending, etc.

In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream.

 ofstream: This Stream class signifies the output file stream and is applied
to create files for writing information to files
 ifstream: This Stream class signifies the input file stream and is applied
for reading information from files
 fstream: This Stream class can be used for both read and write from/to
files.

C++ provides us with the following operations in File Handling:

 Creating a file: open()


 Reading data: read()
 Writing new data: write()
 Closing a file: close()

Common File Modes:

1. ios::in: Open for reading


2. ios::out: Open for writing
3. ios::app: Append mode (write at the end of the file)
4. ios::trunc: If the file exists, its content is deleted before opening
5. ios::binary: Open file in binary mode
6. ios::ate: Open the file and move the file pointer to the end of the file
immediately after opening (useful for reading/writing from the end).
Opening a File
Generally, the first operation performed on an object of one of these classes is
to associate it to a real file. This procedure is known to open a file.

We can open a file using any one of the following methods:


1. First is bypassing the file name in constructor at the time of object creation.
2. Second is using the open() function.

To open a file use

1 open() function
Syntax

#include <fstream>

std::fstream file;

file.open("filename.txt", mode);

1 void open(const char* file_name,ios::openmode mode);


Here, the first argument of the open function defines the name and format of the
file with the address of the file.

The second argument represents the mode in which the file has to be opened.
The following modes are used as per the requirements.

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
Do not Does not allow to create new file if it does not
create exist.
Do not replace Does not replace old file with new file.
noreplace

Example

1 fstream new_file;
2 new_file.open(“newfile.txt”, ios::out);

In the above example, new_file is an object of type fstream, as we know fstream


is a class so we need to create an object of this class to use its member
functions. So we create new_file object and call open() function. Here we use
out mode that allows us to open the file to write in it.

Default Open Modes :

 ifstream - ios::in
 ofstream- ios::out
 fstream - ios::in | ios::out

We can combine the different modes using or symbol | .

Example

ofstream new_file;

1 new_file.open(“new_file.txt”, ios::out | ios::app );


Here, input mode and append mode are combined which represents the file is
opened for writing and appending the outputs at the end.

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 the close() function to close the opened files after the use
of the file.

Using a stream insertion operator << we can write information to a file and
using stream extraction operator >> we can easily read information from a file.
Opening a file in ios::app mode (append mode):

This will append new content to the end of the file without overwriting existing
data.

#include <iostream>

#include <fstream>

using namespace std; // Allows using standard library names without the std::
prefix

int main()

ofstream outfile("example.txt", ios::app);

if (outfile.is_open()) {

outfile << "Appended Text.\n";

outfile.close();

cout << "File appended successfully.\n";

} else {

cout << "Error opening file.\n";

return 0;

OUTPUT
Hello, World!

Appended Text.

You might also like