K. K. Wagh Polytechnic, Nashik.
Program: Information Technology (IF)
Semester: III Scheme: K
Course: OBJECT ORIENTED PROGRAMMING USING C++
Course Code: 313304
Unit-5: File Operations
Topic:5.3 Formatted Input / Output functions in files
Presented by:
Mrs. A. A. Shaikh
Lecturer in Information
Technology
Contents :
•Formatted Input/Output functions in file.
•Detection of End of file.
Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik
Formatted Input / Output Functions in Files:
1. put() - The function put() writes a single
character to the associated stream.
2. get() - The function get() reads a single character
from the associated stream.
3. write() and read() - These functions are designed
to write and read blocks of binary data.
Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik
File Pointers and their Manipulations:
• Each file has 2 pointers known as the file pointers .
• One of them is called the input pointer (or get
pointer)
• And other is called the output pointer (or put
pointer).
• We can use these pointers to move through the files
while reading or writing.
Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik
Default Actions of File Pointers:
• When we open a file in read only mode ,the input pointer
is automatically set at the beginning so that we can read
the file from the start.
• When we open a file in write only mode, the existing
contents are deleted and the output pointer is set at the
beginning . It enables us to write the contents from the
start .
• In case , if we want to open an existing file to add more
data,the file is opened in “append” mode, this moves the
output pointer to the end of the file .
Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik
Open for reading only H E L L O hh
W O R L D
Input Pointer
Open for writing only
Output Pointer
Open in append mode
(for writing more data) H E L L O W O R L D
Output Pointer
Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik
Functions for Manipulations on file pointers:
1. seekg() : Moves get pointer (input) to a specified
location .
2. seekp() : Moves put pointer (output) to a specified
location .
3. tellg() : Gives the current position of the get
pointer.
4. tellp() : Gives the current position of the put
pointer .
Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik
File using open () method and file modes:
• When we want to use modes of files ,so we can write the modes
after file name .
•For ifstream - ios :: in
DEFAULT MODES
•For ofstream - ios:: out
•Syntax - ofstream obj;
• obj.open(“file name”, File mode);
Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik
Detecting End of file:
• Detection of end –of-file condition is
necessary for preventing any further attempt
to read data from the file.
• We can detect when the end of the file is
reached by using the member function eof().
• It returns non-zero value (True) when the
end of file has been reached , otherwise it
returns zero (False).
Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik
Program of Detecting End of File :
#include<iostream.h>
#include<conio.h> if(!readfile.eof())
#include<fstream.h> {
readfile>>name;
void main() readfile>>roll_no;
{ cout<<name<<roll_no;
clrscr(); }
int roll_no; readfile.close();
char name[20]; getch();
}
ofstream outfile;
outfile.open("student",ios::out);
cout<<"enter student name and roll
no:";
cin>>name>>roll_no;
outfile<<"Name:"<<name<<"roll
no"<<roll_no;
outfile.close();
ifstream readfile;
readfile.open("student",ios::in);
Presented by: Mrs. A. A. Shaikh, K. K. Wagh Polytechnic, Nashik