Presentation Transcript
File Handling in C++ :File Handling in C++ Presentation Slides Created By
Gulshan Kumar Hans
PGT Computer Science
K.V. No.3, Jaipur
File Handling :File Handling Introduction to File Handling
Data entered once, required later again
Same Data to be used by others
Data required again by the same program
Files and Streams
Slide 3:Program Files Input
Stream Output
Stream
I/O Streams :I/O Streams Stream Description
cin Standard input stream
cout Standard output stream
cerr Standard error stream
File i/o Streams :File i/o Streams Stream Classes required for File i/o :
ifstream
ofstream
fstream
ifstream :ifstream Input file stream Class
open() is a member function of the class ifstream
Inherited functions of ifstream class, from the class istream are
get()
getline()
read()
seekg()
tellg()
ofstream :ofstream Output file stream Class
open() is a member function of the class ofstream
Inherited functions of ofstream class, from the class ostream are
put()
write()
seekp()
tellp()
fstream :fstream It supports files for simultaneous input and output
fstream is derived from
ifstream
ofstream
iostream
They are parent classes and fstream is the child class
fstream :fstream Member functions of the class fstream
open
close
close all
seekg
seekp
tellg
tellp
Function in C++ :Function in C++ Function:-
A function groups a number of program statements into a unit and gives it a name. The function are used to increase the modularity of the programs and packages. After defining a function, we can call defined function inside the main()
Process for using a function:
Function Prototype (declaring a function)
Function Definition (defining a function with operation statements)
Function Call ( calling a function inside the main)
Slide 11:#include
void line(); //function prototype
void main()
(
cout << “ the value of function is”;
line(); // calling of function
}
void line() // function definition
{
cout<< “welcome”;
}
Slide 12:Data file handling program – (Input and Output with Multiple objects) #include
class person
{
public:
char name[30];
int tel;
void getdata(void)
{
cout>name;
cout>tel;
}
void showdata(void)
{
cout<< “name is”<< name;
cout<<“ tel number is”<< tel;
}
};
Slide 13:Void main()
{
person p;
char ch;
fstream file;
file.open(“test. Txt”, ios::app, ios::in, ios::out);
do
{
cout>ch;
}
while(ch==‘y’ || ch==‘Y’);
file.seekg(0); // reset to start file
file.read( ( char*)&p, size of (p));
Slide 14:while (!file.eof())
{
cout<<“ person is : ”;
p.showdata(); // p is a class object and show data is a function
file.read( (char *) &p, size of (p));
}
}
Slide 15:Take print out of text file and binary file We have speical file name for hardware devices: ofstream outfile;
outfile.open(‘ ‘PRN’ ’); // OPEN FOR PRINTER
Output.put(variable name);
Slide 16:THANKS