Chapter 4 - File
Chapter 4 - File
Chapter 4 - File
Chapter Four
File
So far, we have been using the iostream standard library, which provides cin and cout
object for reading from standard input keyboard and for writing to standard output
console screen respectively. In this section, the method of how to read from a file and
write to a file will be discussed.
Note:- In read operation, a file is considered as standard input and in write operation a
file is used as standard output. In this approach, secondary storage devices are used as
input or output terminal since file is considered to be the content of these devices.
To perform file processing in C++, header files <iostream> and <fstream> must be
included in your C++ source file.
1.1. Category of File
Files in computer systems can be classified into Text file and Binary file. Text files are
those file with specified format or file extension.
Example : .txt, .doc.
This category of files contains printable ASCII characters. The content of this file can be
accessed sequentially. On the other hand files can be stored in binary format that is in
0/1. This category of files can be accessed randomly.
A file must be opened before you can read from it or write to it. Either ofstream or fstream
object may be used to open a file for writing. And ifstream or fstream object is used to
open a file for reading purpose only. Following is the standard syntax for open() function,
which is a member of fstream, ifstream, and ofstream objects.
void open(const char *filename, ios::openmode mode);
Here, the first argument specifies the name and location of the file to be opened and the
second argument of the open() member function defines the mode in which the file
should be opened.
The possible file opening modes are summarized in table below
Page 1
Programming Fundamentals II
When a C++ program terminates it automatically flushes all the streams, release all the
allocated memory and close all the opened files. But it is always a good practice that a
programmer should close all the opened files before program termination.
While doing C++ programming, you write information to a file from your program using
the stream insertion operator (<<) just as we use that operator to output information to
the screen. The only difference is that you use an ofstream or fstream object instead of the
cout object.
The ofstream object outfile is used in the following example to write to the file located at
"C:/Users/UserName/Desktop/cd/adfile.doc" directory.
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
char data[100];
// open a file in write mode.
ofstream outfile;/*the ofstream object outfile used to write to a
file*/
outfile.open("C:/Users/UserName/Desktop/cd/adfile.doc",ios::app);
//uesd to open the file
if(outfile.is_open())
{
cout << "Writing to the file" << endl;
outfile<<"....Hello world.....\n";//this line writes to file
cout << "Enter your name: ";
Page 2
Programming Fundamentals II
cin.getline(data, 100);
// write inputted data into the file.
outfile << data << endl;
cout << "Enter your age: ";
cin >> data;
cin.ignore();
// again write inputted data into the file.
outfile << data << endl;
cout<<"The file is succesfully opend"<<endl;
// close the opened file.
outfile.close();
}
else
{
cout<<"unable to open the file"<<endl;
}
return 0;
}
Note:- This program opens the file and writes the contents with the ofstream object the
close the file finally.
You read information from a file into your program using the stream extraction operator
(>>) just as you use that operator to input information from the keyboard. The only
difference is that you use an ifstream or fstream object instead of the cin object.
// read the data from the file and display it word by word.
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
char data[100];
ifstream infile;
int i=0;
infile.open("C:/Users/UserName/Desktop/afile.doc",ios::in);
//directory
cout << "Reading from the file" << endl;
string str;
while(infile>>data)
{
// read the data from the file and display it word by word.
cout << data<< endl;
}
Page 3
Programming Fundamentals II
Page 4
Programming Fundamentals II
Page 5