Chapter 4 File
Chapter 4 File
1
Objectives
istream ostream
fstream
Chapter 4: File Management 4
Stream hierarchy cont’d
● According to the above hierarchy, the class iostream is derived
from the two classes’ istream and ostream and both istream
and ostream are derived from ios.
● Similarly the class fstream is derived from iostream.
● Generally two main header files are used iostream and
fstream.
● The classes used for input from keyboard and output to the
video display are declared in the header file iostream while the
classes used for disk file input/output are declared in fstream.
● Note that when we include the header file fstream in our
program then there is no need to include iostream header file
as all the classes in fstream are derived from those in iostream
therefore, we can use all the functions of iostream class.
○ If the flow is out of your program, the stream is called an output stream.
● If the input stream flows from the keyboard, then your program will
take input from the keyboard. If the input stream flows from a file, then
your program will take its input from that file.
● Similarly, an output stream can go to the screen or to a file.
ofstream outStream;
Chapter 4: File Management 11
Declaring a stream cont’d
● The types ifstream and ofstream are defined in the
library with the header file fstream, and so any program
that declares stream variables in this way must contain
the following directive (normally near the beginning of
the file):
#include <fstream>
● When using the types ifstream and ofstream, your
program must also contain the following, normally either
at the start of the file or at the start of the function body
that uses the types ifstream or ofstream:
using namespace std;
Mode Description
app Opens file for appending
indata.close();
● The above close() statement will terminate
the relationship between the ifstream object
indata and the file name “c:\myfile.txt”, hence
releasing any resource needed by the system.
Chapter 4: File Management 21
Character IO
● All data is input and output as character data. When your program
outputs the number 10, it is really the two characters '1' and '0' that are
output.
● Similarly, when the user wants to type in the number 10, he or she types
in the character '1' followed by the character '0'. Whether the computer
interprets this 10 as two characters or as the number 10 depends on how
your program is written. But, however your program is written, the
computer hardware is always reading the characters '1' and '0', not the
number 10.
● This conversion between characters and numbers is usually done
automatically so that you need not think about such detail.
● Therefore, C++ provides some low-level facilities for input and output of
character data. These low-level facilities include no automatic conversions.
This allows you to bypass the automatic facilities and do input/output in
absolutely any way you want. You could even write input and output
functions that read and write numbers in Roman numeral notation
Chapter 4: File Management 22
Member functions get and put
● The function get allows your program to read in one character
of input and store it in a variable of type char.
● Every input stream, whether it is an input file stream or the
stream cin, has get as a member function.
● get as a member function of the stream cin, behaves in exactly
the same way for input file streams
● Before now, we have used cin with the extraction operator >>
in order to read a character of input (or any other input, for
that matter). When you use the extraction operator >>, as we
have been doing, some things are done for you automatically,
such as skipping blanks.
● With the member function get, nothing is done automatically. If
you want, for example, to skip over blanks using cin.get, you
must write code to read and discard the blanks.
Chapter 4: File Management 23
Member functions get and put cont’d
● The member function get takes one argument, which should be a variable of
type char. That argument receives the input character that is read from the
input stream (connected to the keyboard or to a file). For example, the
following reads in the next input character from the keyboard and stores it in
the variable nextChar:
char nextChar;
cin.get(nextChar);
● It is important to note that your program can read any character in this way.
If the next input character is a blank, this code will not skip over the blank,
but will read the blank and set the value of nextChar equal to the blank
character.
● If the next character is the new-line character '\n', that is, if the program has
just reached the end of an input line,
andthen
supposethe call
you to cin.get
type in theshown earlier
following two
sets the value of nextChar equal tolines
'\n'.of input to be read by this code:
For example, suppose your program
contains the following code: AB
char c1, c2, c3; CD
cin.get(c1); What will cout<<c3 display?//C? No! Why not? What if
you use cin>>c1>>c2>>c3; and entered the same input
cin.get(c2); from the keyboard? Explain.
cin.get(c3);
}
Chapter 4: File Management 35
File Pointer and their Manipulators
● Each file has two pointers one is called input pointer and second is
output pointer.
● The input pointer is called get pointer and the output pointer is called
put pointer.
● When input and output operation take places, the appropriate pointer
is automatically set according to the access mode.
● For example when we open a file in reading mode, file pointer is
automatically set to the start of the file.
● When we open a file in append mode, the file pointer is automatically
set to the end of file.
● In C++ there are some manipulators by which we can control the
movement of the pointer. The available manipulators are:
seekg()
seekp()
tellg()
tellp()
Chapter 4: File Management 36
● seekg(): this moves get pointer i.e input pointer to a
specified location.
eg. infile.seekg(5);
● seekp(): this move put pointer (output pointer) to a
specified location for example: outfile.seekp(5);
● tellg(): this gives the current position of get pointer
(input pointer)
● tellp(): this gives the current position of put pointer
(output pointer)
eg. ofstream fileout;
fileout.open(“c:\\test.txt”,ios::app);
int length = fileout.tellp();
● By the above statement in length, the total number
bytes of the file are assigned to the integer variable
length. Because the file is opened in append mode
that means, the file pointer is the last part of the file.
Chapter 4: File Management 37
Now lets see the seekg() function in
action
#include<fstream.h>
#include<conio.h>
#include<stdlib.h> //now write the characters to the file
void main() for(ch = 'A'; ch <= 'Z'; ch++)
{
{
fileobj<<ch;
//clrscr(); }
fstream fileobj; fileobj.seekg(8L,ios::beg);//skips eight
char ch; //holds A through Z letters, points to I
fileobj.open("c:\\alph.txt“); fileobj>>ch;
if(fileobj.fail()) cout<<"\nThe 8th character is : "<<ch;
{ fileobj.seekg(16L,ios::beg);//skips 16
letters, points to Q
cerr<<"\nError opening alph.txt";
fileobj>>ch;
getch(); cout<<"\nThe 16th letter is : "<<ch;
exit(1); fileobj.close();
} getch();
}