OObbjjeecctt oorriieenntteedd PPrrooggrraammmmiinngg 
wwiitthh CC++++ 
FFiillee HHaannddlliinngg 
By 
Nilesh Dalvi 
LLeeccttuurreerr,, PPaattkkaarr--VVaarrddee CCoolllleeggee.. 
http://www.slideshare.net/nileshdalvi01
Introduction 
• A file is a collection of related data stored on 
a particular area on the disk. 
• Programs can be designed to perform the 
read and write operations on these files. 
• Programs can involves either or both 
following communication: 
1. Data transfer between console unit and 
program 
2. Data transfer between program and disk file 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Introduction 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Introduction 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Classes for File stream operations 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Classes for File stream operations 
• Includes ifsteam, ofstream and fstream. 
• These classes are derived from fstreambase 
and corrosponding iostream class. 
• Are declared in fstream. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Details of file stream classes 
Class Contents 
filebuf Purpose is to set file buffers to read and write. 
Contains close() and open(). 
fstreambase Provides operations common to the file streams. 
Serves as base for ifsteam, ofstream and fstream. 
Contains close() and open(). 
ifstream Provides input operations 
Contains open() 
Inherits get(), getline(), read(), seekg(), tellg() from istream. 
ofstream Provides output operations 
Contains close() 
Inherits put(), write(), seekp(), tellp() from ostream. 
fstream Provides I/O operations 
Inherits al functions from istream and ostream from iostream. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Opening and closing a File 
• If we want to use a disk file, we need 
following things 
1. Suitable name for file 
2. Data type and structure 
3. Purpose 
4. Opening method 
• A file can be opened in two ways 
1. Using constructor 
2. Using member function open() 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Opening and closing a File 
Opening file using constructor involves 2 steps: 
1. Create a file stream object 
• ofstream is used for o/p stream 
• ifstream is used to create i/p stream 
1. Initialize the file object with desired file name. 
For example, 
ofstream outfile(“results”); // output only 
Similarly, 
ifstream infile(“data”); // input only 
outfile << “Total”; 
outfile << sum; 
infile >> number; 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Opening and closing a File 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include <iostream> 
#include <fstream> 
using namespace std; 
int main () 
{ 
char name[20]; 
int age; 
ofstream out ("text"); 
cout << "n Name: "; 
cin >> name; 
cout << "n Age: "; 
cin >> age; 
out << name<<"t"; 
out << age<<"t"; 
out.close(); 
ifstream in("text"); 
in >> name; 
in >> age; 
cout << "nName t:"<<name<<"n"; 
cout << "Age t:"<<age; 
in.close(); 
return 0; 
} 
Output: 
Name: ABC 
Age: 20 
Name :ABC 
Age :20
Opening and closing a File 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include <iostream> 
#include <fstream> 
using namespace std; 
int main () 
{ 
ofstream fout; 
fout.open("out.txt"); 
char str[300]="Time is a great teacher but 
unfortunately it kills all its pupils."; 
fout<<str; 
fout.close(); 
return 0; 
}
Opening and closing a File 
#include <iostream> 
#include <fstream> 
using namespace std; 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
int main () 
{ 
ifstream fin; 
fin.open("out.txt"); 
char ch; 
while(fin) 
{ 
fin.get(ch); 
cout<<ch; 
} 
fin.close(); 
return 0; 
}
Checking for errors 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include <iostream> 
#include <fstream> 
using namespace std; 
int main () 
{ 
ifstream fin; 
char *f_name = "out1.txt"; 
fin.open(f_name); 
char ch; 
if(!fin) 
{ 
cerr << "Error in opening file: "<< f_name << endl; 
return 1; 
} 
while(fin) 
{ 
fin.get(ch); 
cout<<ch; 
} 
fin.close(); 
return 0; 
}
Finding end of file 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include <iostream> 
#include <fstream> 
using namespace std; 
int main () 
{ 
ifstream fin; 
char *f_name = "afile"; 
fin.open(f_name); 
char ch; 
if(!fin) 
{ 
cerr << "Error in opening file: "<< f_name << endl; 
return 1; 
} 
while(fin.eof()==0) 
{ 
fin.get(ch); 
cout<<ch; 
} 
cout << endl; 
fin.close(); 
return 0; 
}
File opening modes 
• Opening file also involves several modes 
depending upon operation to be carried out 
with the file. 
• Syntax of open() function 
object.open(“file_name”, mode); 
• Here, mode in which file is to be opened. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
File opening modes 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
Mode 
Parameter 
Operation 
ios::app Append data at the end of file 
ios::ate After opening character pointer goes to 
end of file 
ios::binary File open in binary mode 
ios::in Open file for reading only 
ios::out Open file for writing only 
ios::nocreate Open unsuccessful if the file does not exist 
ios::noreplace Open unsuccessful if the file already exist 
ios::trunc Delete the contents of the file if it exist
File opening modes 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include <iostream> 
#include <fstream> 
using namespace std; 
int main () 
{ 
char data[100],ch; 
ofstream out; 
out.open("text",ios::out | ios::binary); 
cout << "Enter the text: " << endl; 
cin >> data; 
out << data; 
out.close(); 
cout << "Content of the file: "<<endl; 
ifstream fin; 
fin.open("text",ios::in | ios::binary); 
fin.get(ch); 
while(fin.eof()==0) 
{ 
cout<<ch; 
fin.get(ch); 
} 
cout << endl; 
fin.close(); 
return 0; 
} 
Output: 
Enter the text: 
Programming 
Content of the file: 
Programming
File pointers and Manipulators 
• All file objects hold two file pointers 
associated with the file. 
• (input) get pointer helps in reading the file 
from given location. 
• (output) put pointer helps for writing data 
in the file at specified location. 
• They are by default set at the beginning of 
the file. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
File pointers and Manipulators 
• To explicitly set stream classes provide following 
functions: 
• Read mode when file is opened in read mode, get 
pointer is set at the beginning of the file. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
File pointers and Manipulators 
• write mode when file is opened in write mode, put 
pointer is set at the beginning of the file. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
File pointers and Manipulators 
• Append mode this mode allows to add data at the 
end of the file. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
File pointers handling functions 
Function Uses Remark 
tellg() gives the current position of the get 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
pointer 
Member of ifstream class 
seekg() moves get pointer(input) to a specified 
location 
Member of ifstream class 
tellp() gives the current position of the put 
pointer 
Member of ofstream class 
seekp() moves put pointer (output) to a specified 
location 
Member of ofstream class 
• seekg() and tellg() are the member functions of 
ifstream. 
• seekp() and tellp() are the member functions of 
ofstream.
File pointers and Manipulators 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include <iostream> 
#include <fstream> 
using namespace std; 
int main () 
{ 
char data[32]; 
int r; 
ofstream out; 
out.open("text",ios::out); 
cout << "Enter the text: " << endl; 
cin.getline(data, 20); 
out << data; 
out.close(); 
ifstream fin; 
cout << "Content of the file: "<<endl; 
fin.open("text",ios::in); 
while(fin.eof()==0) 
{ 
fin >> data; 
cout << data; 
r = fin.tellg(); 
cout << "("<< r <<")"; 
} 
fin.close(); 
return 0; 
} 
Output: 
Enter the text: 
Programming with C++ 
Content of the file: 
Programming(11)with(16)C+(19)
Manipulators with Arguments 
• seekp() and seekp() are used with two arguments 
seekg(offset, pre_position); 
seekp(offset, pre_position); 
• offset – number of bytes the file pointer is to be 
shifted from the arguments pre_position. 
• offset must be positive or negative number. 
• pre_position have one of following: 
– ios::beg beginning of the file 
– ios::cur Current position of the file pointer 
– ios::end End of the file. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
File pointers and Manipulators 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include <fstream> 
#include <iostream> 
using namespace std; 
int main() 
{ 
ofstream out("mydoc.txt",ios::binary); 
char text[80],ch; 
strcpy(text,"Hi this is a test file!"); 
out<<text; 
out.close(); 
ifstream in("mydoc.txt",ios::binary); 
in.seekg(8); 
cout<<endl<<"Starting from position 8 the contents are:"<<endl; 
while (!in.eof()) 
{ 
in.get(ch); 
if (!in.eof()) 
{ 
cout<<ch; 
} 
} 
in.close(); 
return 0; 
} 
Output: 
Starting from position 8 the contents are: 
is a test file!
File pointers and Manipulators 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include <iostream> 
#include <fstream> 
using namespace std; 
int main () 
{ 
char data[32],ch; 
int r; 
ofstream out; 
out.open("text",ios::out); 
cout << "Enter the text: " << endl; 
cin.getline(data, 20); 
out << data; 
out.close(); 
ifstream in; 
cout << "Reverse Contents of the file: "<<endl; 
in.open("text",ios::in); 
in.seekg(0,ios::end); 
int m = in.tellg(); 
for(int i = 1; i <= m; i++) 
{ 
in.seekg(-i, ios::end); 
in >> ch; 
cout << ch; 
} 
in.close(); 
return 0; 
} 
Output: 
Enter the text: 
Happy 
Reverse Contents of the file: 
yppaH
File pointers and Manipulators 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include <iostream> 
#include <fstream> 
using namespace std; 
int main () 
{ 
char data[30],ch; 
int r; 
ofstream out; 
out.open("text",ios::out); 
cout << "Enter the text: " << endl; 
cin.getline(data, 20); 
out << data; 
out.seekp(0, ios::beg); 
cout << "Enter the text to replace 1st word of 1st text:: "<<endl; 
cin.getline(data,20); 
out << data; 
out.close(); 
ifstream in; 
cout << "Contents of the file: "<<endl; 
in.open("text",ios::in); 
while(in.eof()==0) 
{ 
in.getline(data,20); 
cout << data; 
} 
in.close(); 
return 0; 
} 
Output:: 
Enter the text: 
kk ll mm nn 
Enter the text to replace 1st word of 1st text:: 
oo 
Contents of the file: 
oo ll mm nn
Sequential read and write operations 
put() and get() function 
• the function put() writes a single character 
to the associated stream. 
• the function get() reads a single character 
form the associated stream. 
• Example : 
file.get(ch); 
file.put(ch); 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Sequential read and write operations 
#include <iostream> 
#include <fstream> 
using namespace std; 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
int main() 
{ 
char c; 
ofstream ofs("file.txt"); 
do 
{ 
c = cin.get(); 
ofs.put(c); 
}while (c != '!'); 
ofs.close(); 
return 0; 
}
Sequential read and write operations 
write() and read() function 
• write() and read() functions write and read blocks 
of binary data. 
• Syntax: 
file.read((char *)&P, sizeof(P)); 
file.write((char *)&P, sizeof(P)); 
• Two parameters, first is address of the variable and 
second is size of variable P in bytes. 
• Address is converted to char type. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Sequential read and write operations 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include <fstream> 
#include <iostream> 
using namespace std; 
int main() 
{ 
int marks[5] = {10, 20, 30, 40, 50}; 
ofstream ofs("marks.txt"); 
ofs.write((char *) & marks, sizeof (marks)); 
ofs.close(); 
for (int i = 0; i < 5; i++) 
{ 
marks[i] = 0; 
} 
ifstream ifs("marks.txt"); 
ifs.read((char *) & marks, sizeof (marks)); 
ifs.close(); 
for (int i = 0; i < 5; i++) 
{ 
cout << "marks[" << i << "] = " << marks[i] << endl; 
} 
return 0; 
} 
Output:: 
marks[0] = 10 
marks[1] = 20 
marks[2] = 30 
marks[3] = 40 
marks[4] = 50
Error Handling Functions 
FUNCTION RETURN VALUE AND MEANING 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
eof() 
returns true (non zero) if end of file is 
encountered while reading; otherwise 
return false(zero) 
fail() 
return true when an input or output 
operation has failed 
bad() 
returns true if an invalid operation is 
attempted or any unrecoverable error has 
occurred. 
good() returns true if no error has occurred.
Random Access Operation 
• Random-access files enable you to read or 
write records in any order. 
• Even if you want to perform sequential 
reading or writing of the file, you can use 
random-access processing and randomly 
read or write the file sequentially from the 
first record to the last. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Random Access Operation 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
File handling

File handling

  • 1.
    OObbjjeecctt oorriieenntteedd PPrrooggrraammmmiinngg wwiitthh CC++++ FFiillee HHaannddlliinngg By Nilesh Dalvi LLeeccttuurreerr,, PPaattkkaarr--VVaarrddee CCoolllleeggee.. http://www.slideshare.net/nileshdalvi01
  • 2.
    Introduction • Afile is a collection of related data stored on a particular area on the disk. • Programs can be designed to perform the read and write operations on these files. • Programs can involves either or both following communication: 1. Data transfer between console unit and program 2. Data transfer between program and disk file Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 3.
    Introduction Nilesh Dalvi,Lecturer@Patkar-Varde College, Goregaon(W).
  • 4.
    Introduction Nilesh Dalvi,Lecturer@Patkar-Varde College, Goregaon(W).
  • 5.
    Classes for Filestream operations Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 6.
    Classes for Filestream operations • Includes ifsteam, ofstream and fstream. • These classes are derived from fstreambase and corrosponding iostream class. • Are declared in fstream. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 7.
    Details of filestream classes Class Contents filebuf Purpose is to set file buffers to read and write. Contains close() and open(). fstreambase Provides operations common to the file streams. Serves as base for ifsteam, ofstream and fstream. Contains close() and open(). ifstream Provides input operations Contains open() Inherits get(), getline(), read(), seekg(), tellg() from istream. ofstream Provides output operations Contains close() Inherits put(), write(), seekp(), tellp() from ostream. fstream Provides I/O operations Inherits al functions from istream and ostream from iostream. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 8.
    Opening and closinga File • If we want to use a disk file, we need following things 1. Suitable name for file 2. Data type and structure 3. Purpose 4. Opening method • A file can be opened in two ways 1. Using constructor 2. Using member function open() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 9.
    Opening and closinga File Opening file using constructor involves 2 steps: 1. Create a file stream object • ofstream is used for o/p stream • ifstream is used to create i/p stream 1. Initialize the file object with desired file name. For example, ofstream outfile(“results”); // output only Similarly, ifstream infile(“data”); // input only outfile << “Total”; outfile << sum; infile >> number; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 11.
    Opening and closinga File Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <iostream> #include <fstream> using namespace std; int main () { char name[20]; int age; ofstream out ("text"); cout << "n Name: "; cin >> name; cout << "n Age: "; cin >> age; out << name<<"t"; out << age<<"t"; out.close(); ifstream in("text"); in >> name; in >> age; cout << "nName t:"<<name<<"n"; cout << "Age t:"<<age; in.close(); return 0; } Output: Name: ABC Age: 20 Name :ABC Age :20
  • 12.
    Opening and closinga File Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <iostream> #include <fstream> using namespace std; int main () { ofstream fout; fout.open("out.txt"); char str[300]="Time is a great teacher but unfortunately it kills all its pupils."; fout<<str; fout.close(); return 0; }
  • 13.
    Opening and closinga File #include <iostream> #include <fstream> using namespace std; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). int main () { ifstream fin; fin.open("out.txt"); char ch; while(fin) { fin.get(ch); cout<<ch; } fin.close(); return 0; }
  • 14.
    Checking for errors Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <iostream> #include <fstream> using namespace std; int main () { ifstream fin; char *f_name = "out1.txt"; fin.open(f_name); char ch; if(!fin) { cerr << "Error in opening file: "<< f_name << endl; return 1; } while(fin) { fin.get(ch); cout<<ch; } fin.close(); return 0; }
  • 15.
    Finding end offile Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <iostream> #include <fstream> using namespace std; int main () { ifstream fin; char *f_name = "afile"; fin.open(f_name); char ch; if(!fin) { cerr << "Error in opening file: "<< f_name << endl; return 1; } while(fin.eof()==0) { fin.get(ch); cout<<ch; } cout << endl; fin.close(); return 0; }
  • 16.
    File opening modes • Opening file also involves several modes depending upon operation to be carried out with the file. • Syntax of open() function object.open(“file_name”, mode); • Here, mode in which file is to be opened. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 17.
    File opening modes Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Mode Parameter Operation ios::app Append data at the end of file ios::ate After opening character pointer goes to end of file ios::binary File open in binary mode ios::in Open file for reading only ios::out Open file for writing only ios::nocreate Open unsuccessful if the file does not exist ios::noreplace Open unsuccessful if the file already exist ios::trunc Delete the contents of the file if it exist
  • 18.
    File opening modes Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <iostream> #include <fstream> using namespace std; int main () { char data[100],ch; ofstream out; out.open("text",ios::out | ios::binary); cout << "Enter the text: " << endl; cin >> data; out << data; out.close(); cout << "Content of the file: "<<endl; ifstream fin; fin.open("text",ios::in | ios::binary); fin.get(ch); while(fin.eof()==0) { cout<<ch; fin.get(ch); } cout << endl; fin.close(); return 0; } Output: Enter the text: Programming Content of the file: Programming
  • 19.
    File pointers andManipulators • All file objects hold two file pointers associated with the file. • (input) get pointer helps in reading the file from given location. • (output) put pointer helps for writing data in the file at specified location. • They are by default set at the beginning of the file. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 20.
    File pointers andManipulators • To explicitly set stream classes provide following functions: • Read mode when file is opened in read mode, get pointer is set at the beginning of the file. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 21.
    File pointers andManipulators • write mode when file is opened in write mode, put pointer is set at the beginning of the file. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 22.
    File pointers andManipulators • Append mode this mode allows to add data at the end of the file. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 23.
    File pointers handlingfunctions Function Uses Remark tellg() gives the current position of the get Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). pointer Member of ifstream class seekg() moves get pointer(input) to a specified location Member of ifstream class tellp() gives the current position of the put pointer Member of ofstream class seekp() moves put pointer (output) to a specified location Member of ofstream class • seekg() and tellg() are the member functions of ifstream. • seekp() and tellp() are the member functions of ofstream.
  • 24.
    File pointers andManipulators Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <iostream> #include <fstream> using namespace std; int main () { char data[32]; int r; ofstream out; out.open("text",ios::out); cout << "Enter the text: " << endl; cin.getline(data, 20); out << data; out.close(); ifstream fin; cout << "Content of the file: "<<endl; fin.open("text",ios::in); while(fin.eof()==0) { fin >> data; cout << data; r = fin.tellg(); cout << "("<< r <<")"; } fin.close(); return 0; } Output: Enter the text: Programming with C++ Content of the file: Programming(11)with(16)C+(19)
  • 25.
    Manipulators with Arguments • seekp() and seekp() are used with two arguments seekg(offset, pre_position); seekp(offset, pre_position); • offset – number of bytes the file pointer is to be shifted from the arguments pre_position. • offset must be positive or negative number. • pre_position have one of following: – ios::beg beginning of the file – ios::cur Current position of the file pointer – ios::end End of the file. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 26.
    File pointers andManipulators Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <fstream> #include <iostream> using namespace std; int main() { ofstream out("mydoc.txt",ios::binary); char text[80],ch; strcpy(text,"Hi this is a test file!"); out<<text; out.close(); ifstream in("mydoc.txt",ios::binary); in.seekg(8); cout<<endl<<"Starting from position 8 the contents are:"<<endl; while (!in.eof()) { in.get(ch); if (!in.eof()) { cout<<ch; } } in.close(); return 0; } Output: Starting from position 8 the contents are: is a test file!
  • 27.
    File pointers andManipulators Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <iostream> #include <fstream> using namespace std; int main () { char data[32],ch; int r; ofstream out; out.open("text",ios::out); cout << "Enter the text: " << endl; cin.getline(data, 20); out << data; out.close(); ifstream in; cout << "Reverse Contents of the file: "<<endl; in.open("text",ios::in); in.seekg(0,ios::end); int m = in.tellg(); for(int i = 1; i <= m; i++) { in.seekg(-i, ios::end); in >> ch; cout << ch; } in.close(); return 0; } Output: Enter the text: Happy Reverse Contents of the file: yppaH
  • 28.
    File pointers andManipulators Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <iostream> #include <fstream> using namespace std; int main () { char data[30],ch; int r; ofstream out; out.open("text",ios::out); cout << "Enter the text: " << endl; cin.getline(data, 20); out << data; out.seekp(0, ios::beg); cout << "Enter the text to replace 1st word of 1st text:: "<<endl; cin.getline(data,20); out << data; out.close(); ifstream in; cout << "Contents of the file: "<<endl; in.open("text",ios::in); while(in.eof()==0) { in.getline(data,20); cout << data; } in.close(); return 0; } Output:: Enter the text: kk ll mm nn Enter the text to replace 1st word of 1st text:: oo Contents of the file: oo ll mm nn
  • 29.
    Sequential read andwrite operations put() and get() function • the function put() writes a single character to the associated stream. • the function get() reads a single character form the associated stream. • Example : file.get(ch); file.put(ch); Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 30.
    Sequential read andwrite operations #include <iostream> #include <fstream> using namespace std; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). int main() { char c; ofstream ofs("file.txt"); do { c = cin.get(); ofs.put(c); }while (c != '!'); ofs.close(); return 0; }
  • 31.
    Sequential read andwrite operations write() and read() function • write() and read() functions write and read blocks of binary data. • Syntax: file.read((char *)&P, sizeof(P)); file.write((char *)&P, sizeof(P)); • Two parameters, first is address of the variable and second is size of variable P in bytes. • Address is converted to char type. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 32.
    Sequential read andwrite operations Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <fstream> #include <iostream> using namespace std; int main() { int marks[5] = {10, 20, 30, 40, 50}; ofstream ofs("marks.txt"); ofs.write((char *) & marks, sizeof (marks)); ofs.close(); for (int i = 0; i < 5; i++) { marks[i] = 0; } ifstream ifs("marks.txt"); ifs.read((char *) & marks, sizeof (marks)); ifs.close(); for (int i = 0; i < 5; i++) { cout << "marks[" << i << "] = " << marks[i] << endl; } return 0; } Output:: marks[0] = 10 marks[1] = 20 marks[2] = 30 marks[3] = 40 marks[4] = 50
  • 33.
    Error Handling Functions FUNCTION RETURN VALUE AND MEANING Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). eof() returns true (non zero) if end of file is encountered while reading; otherwise return false(zero) fail() return true when an input or output operation has failed bad() returns true if an invalid operation is attempted or any unrecoverable error has occurred. good() returns true if no error has occurred.
  • 34.
    Random Access Operation • Random-access files enable you to read or write records in any order. • Even if you want to perform sequential reading or writing of the file, you can use random-access processing and randomly read or write the file sequentially from the first record to the last. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 35.
    Random Access Operation Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).