Institute - Uie Department-Academic Unit-1
Institute - Uie Department-Academic Unit-1
3
INTRODUCTION
Working with files generally requires the following kinds of data communication methodologies:
• Data transfer between console units
• Data transfer between the program and the disk file
So far we have learned about iostream standard library which provides cin and cout methods for reading from standard input
and writing to standard output respectively. In this chapter, you will get to know how files are handled using C++ program and
what are the functions and syntax used to handle files in C++.
4
FILE HANDLING
A C++ stream is a flow of data into or out of a program, such as the data written to cout or read from cin.
For this class we are currently interested in four different classes:
• istream is a general purpose input stream. cin is an example of an istream.
• ostream is a general purpose output stream. cout and cerr are both examples of ostreams.
• ifstream is an input file stream. It is a special kind of an istream that reads in data from a data file.
• ofstream is an output file stream. It is a special kind of ostream that writes data out to a data file.
5
FILE HANDLING
• Files are used to store data in a storage device permanently.
• File handling provides a mechanism to store the output of a program in a file and to perform various operations on it.
• A stream is an abstraction that represents a device on which operations of input and output are performed. A stream can be
represented as a source or destination of characters of indefinite length depending on its usage.
• 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.
6
STREAM CLASS HEIRARCHY
• Files are used to store data in a storage device permanently.
8
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 by passing the file name in constructor at the time of object creation.
2. Second is using the open() function.
To open a file, use open() function
Syntax
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.
9
OPENING A FILE- OPENING MODES
The following modes are used as per the requirements.
Example:
fstream new_file;
new_file.open(“newfile.txt”,
ios::out);
• In the above example we first create an object to class fstream and name it ‘new_file’.
• Then we apply the open() function on our ‘new_file’ object.
• We give the name ‘new_file’ to the new file we wish to create and we set the mode to ‘out’ which allows us to write in our file.
• We use a ‘if’ statement to find if the file already exists or not if it does exist then it will going to print “File creation failed” or
it will gonna create a new file and print “New file created”.
12
Example of writing to a file using the open()
function
Explanation
Here we first create a new file
“new_file_write” using open()
function since we wanted to
send output to the file so, we
use ios::out. As given in the
program, information typed
inside the quotes after
Insertion Pointer “<<” got
passed to the output file.
ifstream fin;
ofstream fout; // file opening routine
fin.seekg(30); // will move the get_pointer (in ifstream) to byte number 30 in the file
fout.seekp(30); // will move the put_pointer (in ofstream) to byte number 30 in the file
23
APPLICATIONS
• File handling functions are greatly used in building real life projects.
24
REFERENCES
• Reference Website
[1] https://www.edureka.co/blog/file-handling-in-cpp/
[2] https://www.studytonight.com/cpp/file-streams-in-cpp.php
[3] http://www.infobrother.com/Tutorial/C++/C++-Random-files
[4] https://codescracker.com/cpp/cpp-file-pointers-random-access.htm
25
THANK YOU
For queries
Email: CST157_2019@gmail.com