Professional Documents
Culture Documents
Page 2 of 5
in : Read only mode. Used for reading purposes. This is default file mode for ifstream class
with this mode an existing file is opened with all its data intact. But if the file doesnt exist then a
blank file will be opened.
out: Write only mode. Used for writing purposes. This is default file mode for ofstream
class. With this mode an existing file with same name is replaced with a new blank file and the
blank file is opened. In such a situation we will lost the old existing file. If the file doesnt exit
straight way a new blank is opened.
app: To append (add) data at the end of the file. This mode is used along with out mode.
ate: To fix the file pointer at the end the of file when it is opened. With this we write data
any where in the file even on the existing data by shifting the file pointer position.
nocreate: To open only an existing file. This mode can be used along with both in and out
modes.
noreplace: To open only a non-existing file ( i.e. only a new file). This mode can be used
along with both in and out modes.
trunc: With this mode all the data of the file is deleted and a blank file is opened.
From the above description of file modes we can also conclude that we can use one or more than
file modes when we create or open a file. In the above program file opening statements can also be
given as
Progam#1 : Program for reading a file
Program#2 : Program for writing on to a file
ifstream aFile(StName.txt, ios::in); //
ofstream aFile(StName.txt, ios::out); //
opening a
opening
file
a file
Few examples of file opening statements:
ifstream aFile(StName.txt);
Here file mode is ios::in which has been passed implicitly.
ifstream aFile(StName.txt, ios::in);
Here explicitly file mode ios::in has been specified.
ofstream X(Story.dat);
Here file mode is ios::out which has been passed implicitly. Here a new blank Story.txt
file will be created and if it is already existing then all the data of the file will be lost.
ofstream X(Story.dat,ios::out);
Here file mode is ios::out which has been passed explicitly. Here a new blank Story.txt
file will be created and if it is already existing then all the data of the file will be lost.
fstream X(Story.dat);
This one is a wong Satement as we have not given the file mode. It is important to note
that whenever we open or create a file with the help of fstream class we must always pass
in, out or in out (both) file modes. i.e. there is no default file mode for the class fstream.
Correct statement for the above will be as
fstream X(Story.dat,ios::in||ios::out); // Here we can both read from and write onto
the file
fstream X(Story.dat,ios::in||ios::nocreate);
Here a Story.dat will be opened for reading but only when the file is existing.
fstream X(Story.dat,ios::out||ios::noreplace);
Here a new Story.dat will be opened for writing but only when the file is non-existing.
The entire above said concept works equally good for binary also.
Uses of cascaded operator (<< & >>) for reading and writing onto a text file:
Perhaps the easiest method to read and write from/to a file is with the help of cascaded <<
(insertion) and >>(extraction) operators.
<< : Used for writing onto the file
>> : Used for reading from the file
The following shows how we use these two operators.
Page 3 of 5
Progam#3 : Program for reading a file
#include <fstream.h>
void main()
{
ifstream X(MyFile.txt); // Opening the file
char *s;
while(!X.eof())
{ X>>s; // reading from the file
cout<<s; }
X.close();
}
Functions like: open(), close(), get(), getline(), put(), tellg(), seekg(), tellp() and
seekp()
Open(): This is member of all three classes we are using in file handling namely ifstream, of
stream and
fstream. We can open/create a file in ways. Either using the constructors or using the open()
function.
e.g. i) ifstream X(Story.txt); // This method uses a constructor of class ifstream to create the
file
ii) ifstream X;
X.open(Story.txt); // This method uses member function open() of class ifstream to
create the
file
close() : This one is also member of three classes namely ifstream, ofstream and fstream. This is
used to close a file.
e.g. ofstream X(Story.txt);
..
X.close();
get(): This function is used to read a character from a file. It is member of ifstream class.
e.g. ifstream X(Story.txt);
char ch;
X.get(ch);
getline(): This is used for reading a line (string) from standard input device. This function receives
three parameters and their uses are as follows:
First parameter is a storage variable name where the string is stored.
Second parameter specifies the the length of the string to be read from the input buffer.
Third parameter is a delimiting character that specifies that after this character no other
character from the input buffer will be read.
This function can also be used for reading from a file.
e.g. char *s;
cin.getline(s,20,*);
Case1
If we enter We are Indians. Then s will store We are Indians.
Case2
If we enter We are*Indians. Then s will store We are*
Case3
If we enter We are Indians. And we are proud of it. Then s will store We are
Indians. And ie 20 chars.
Case4
If we enter We are Indians. And we are proud*of it. Then s will store We are
Indians. And ie 20 chars.
put(): This one is a member of ofstream class and is used for writing a single character on to a
file.
e.g. ofstream X(Story.txt);
Page 4 of 5
X.put(P);
tellg(): It gives the current get-file pointer (read mode) position in a data file.
tellp(): It gives the current put-file pointer (write mode) position in a data file.
seekg(): This is used to shift get-file pointer position in a data file.
e.g. ifstream X(Story.txt);
.
X.seekg(10);
Here file pointer X will be shifted 10 bytes in forward direction. To move the file pointer in
backward direction we pass a ve no. This function receives one more parameter known as offset
that can be any of the three namely ios::beg, ios::end, and ios::cur.
ios::beg : To move the pointer from the beginning of the file.
ios::end : To move the pointer from the end of the file.
ios::cur : To move the pointer from the current position of the file pointer.
e.g. X.seekg(10,ios::beg); 10 bytes form the beginning of the file.
seekp(): Same as above only that it works with put-file pointer and is a member of ofstream
class.
Detecting end of file with or without using eof() function:
A file pointer in a data file keeps on moving or changing its position with every read(get-file
pointer) or write(put-file pointer) operation. Following are the methods to find whether the file
pointer has reached to end of the file or not.
Program#5 : Detecting EOF with eof()
Program#6 : Detecting EOF without eof()
#include <fsteam.h> // Using header file
#include <fstream.h> // Using header file
void main()
void main()
{ ifstream aFile(StName.txt); // opening a
{ ifstream aFile(StName.txt); // opening a
file
file
char ch;
char ch;
while(!aFile.eof()) // checking for EOF
while(aFile) // checking for EOF
{
aFile.get(ch); // reading file
{
aFile.get(ch); // reading file
cout<<ch; // displaying read data
cout<<ch; // displaying read data
}
}
aFile.close();
aFile.close();
}
}
In program#5 aFile.eof() returns true if aFile is at the EOF or false otherwise. And therefore we are
using while (!aFile.eof()) so that until and unless aFile is at EOF while loop will iterate. Here note
the ! operatetor.
In program#6 aFile returns false if aFile is at the EOF or true otherwise. And therefore we are using
while (aFile) so that until and unless aFile is at EOF while loop will iterate.
Page 5 of 5
Questions:
Q1. Differentiate between the following
a) ios::app and ios::ate b) ios::nocreate and ios::noreplace c) get() and put()
d) ios::trunc and ios::out e) tellg() and seekg()
Q2. What is file stream object or file pointer?
Q3. What is a stream?
Q4. Given the class definition
class STOCK
{ int ITNO; char ITEM[10];
public:
void GETIT() { cin>>ITNO; gets(ITEM); }
void SHOIT() { cout<<ITNO<< <<ITEM <<endl; }
};
Fill in the blanks for the following program:
Void read_obj()
{ ifstream X;
X.open(stock.dat,___::out || __::in);
If(!X)
{ cout<<\n Cannot open file; exit(1); }
STOCK sk;
While(!X.____)
{
X.read( ( _____ )&sk,sizeof( _____ ));
SHOWIT();
}
X.close();
}
Q5. Assuming that a text file named FIRST.TXT contains some text written into it, write a
function named vowelwords(), that reads the files FIRST.TXT and creates a new file named
SECOND.TXT, to contain only those words from the file FIRST.TXT which start with a lower case
vowel ( ie a,e,i,o ,u). For example, if the file FIRST.TXT contains Carry umbrella and overcoat
when it rains Then the second file should contain umbrella and overcoat it
Hint: Here you have to read file first.txt word by word and check for the first character whether it
is lowercase vowel or not. If it is then you have to write word on to the file second.txt. For this
purpose you should use >> operator for reading first.txt and << operator for writing on to the
file second.txt.
Q3. Write a c++ program to replace each space of text file Books.dat with *.
Hint: Here file has to be read character by character and if the character is a space then we have
to write on to the same place with * character. For this purpose open the file in R/W mode. And
use seekg() or seekp() function properly to write on the correct place of the file.