Professional Documents
Culture Documents
Chapter Seven - File Operations
Chapter Seven - File Operations
Computer Programming
3
Introduction
In any operation including programming world, files
are vital as they store data.
This chapter discuss working of file handling in C+
+. Following pointers will be covered in the article,
Opening a File
Writing to a File
Reading from a File
Close a File
4
File Handling In C++ - Stream
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.
5
File Handling In C++ - Stream
C++ provides the following classes to perform
output and input of characters to/from files:
◦ ofstream: Stream class to write on files
◦ ifstream: Stream class to read from files
◦ fstream: Stream class to both read and write from/to files.
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
File Handling In C++ - Stream
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()
Using the above methods we can read or write on
file.
7
Opening File
The first operation generally performed on an object of one of
these classes is to associate it to a real file.
This procedure is known as to open a file.
An open file is represented within a program by a stream
In order to open a file with a stream object we use its member
function open:
open (filename, mode);
Where filename is a string representing the name of the file to be
opened, and mode is an optional parameter with a combination of
the following flags:
8
Opening File
Example
◦ fstream new_file;
◦ new_file.open(“newfile.txt”, ios::out);
Each of the open member functions of classes ofstream,
ifstream and fstream has a default mode that is used if the
file is opened without a second argument:
CLASS Default mode parameter
ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
11
Example of opening/creating a file using the open() function
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
fstream new_file;
new_file.open("new_file",ios::out);
if(!new_file)
{
cout<<"File creation failed";
}
else
{
cout<<"New file created";
new_file.close(); // Step 4: Closing file
}
return 0;
}
12
Writing to a File
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream new_file;
new_file.open("new_file_write.txt",ios::out);
if(!new_file)
{
cout<<"File creation failed";
}
else
{
cout<<"New file created";
new_file<<"Learning File handling"; //Writing to file
new_file.close(); }
return 0;
}
13
Writing from a File
Explanation
14
Reading from a File
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream new_file;
new_file.open("new_file_write.txt",ios::in);
if(!new_file)
cout<<"No such file"; }
else { char ch; while (!new_file.eof())
{ new_file >>ch;
cout << ch;
}
new_file.close();
return 0;
}
15
Reading from a File
Explanation
16
Closing a file
When we are finished with our input and output operations
on a file we shall close it so that the operating system is
notified and its resources become available again.
For that, we call the stream's member function close.
This member function takes flushes the associated buffers
and closes the file:
myfile.close();
The stream object can be re-used to open another file, and
the file is available again to be opened by other processes.
17
Closing a file
Example
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream new_file;
new_file.open("new_file.txt",ios::out);
new_file.close();
return 0;
}
18
Checking state flags
19
End of Chapter Six
Thank You
20