FILE HANDLING IN C ++
Files are used to permanently store different types of data in secondary memory devices, such (Hard drive, pen drive, floppy disk, etc.)
In C++, variables store values just for temporary use, so if you want to store your data permanently then you use the files.
FILES HANDLING CLASSES
FILES HANDLING CLASSES
Class
|
Function
|
ofstream
|
This class is only used to write (store data) in files.
|
ifstream
|
This class is only used to read (access data) from files.
|
fstream
|
This class is used to both read and write (store and access data) from/to files.
|
FILE MODES
out: to write data in file.
in: to read data from a file.
app: to write data in append mode
PROGRAM TO WRITE A FILE
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
// Create ofstream class object and connect it with "Record" file
ofstream ob("Record.txt",ios::out);
// Write text in file
ob<<"My first file";
cout<<"File has been written successfully ...";
getch();
}
//Create ifstream class object and connect it with "Record" file
EXAMPLE OF OPEN FUNCTION
Note: in the above code "ofstream" is a file class and "ob" its object.ob access open function by . (dot) operator.
EXAMPLE OF CLOSE FUNCTION
// Open record.dat file in append mode
while(ob.eof()==0)
SEEKP FUNCTION
ofstream ob;
ob.seekp(5); // put cursor in fifth position in abc.dat file
SEEKG FUNCTION
// An example of seekg function
/* g stands for get and its used to get characters from a file
with desired poistion of cursor */
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
void main()
{
clrscr();
char c;
ifstream ob;
ob.open("abc.dat",ios::in);
ob.seekg(8); // get cursor in 8 position in abc.dat file
while(ob) //ob.eof()==0
{
ob.get(c);
cout<<c;
}
ob.close();
getch();
}
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
// Create ofstream class object and connect it with "Record" file
ofstream ob("Record.txt",ios::out);
// Write text in file
ob<<"My first file";
cout<<"File has been written successfully ...";
getch();
}
Program Explanation: In the above program "Ofstream" is a predefined constructor that is used used to write contents in a file, "Record.txt" is a text file and "in" is writing mode.
PROGRAM READ CONTENT FROM AN EXISTING FILE
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
char c;
//Create ifstream class object and connect it with "Record" file
ifstream ob("record.txt",ios::in);
while(ob.eof()==0)
{
ob.get(c);
cout<<c;
}
ob.close();
getch();
}
OPEN AND CLOSE FUNCTION
The open and close functions are used to read and write two or more files within one program.
EXAMPLE OF OPEN FUNCTION
ofsteram ob;
ob.open("Record.dat",ios::app);
Note: in the above code "ofstream" is a file class and "ob" its object.ob access open function by . (dot) operator.
EXAMPLE OF CLOSE FUNCTION
//An example of fstream obejct
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
void main()
{
clrscr();
char s[60];
// Create an object of fstream class
fstream ob;
// Open record.dat file in append mode
ob.open("record.dat",ios::app);
cout<<"Enter any sentece....\n";
gets(s);
// Writing in file
ob<<s;
//close the file
ob.close();
// open record.dat file in reading mode
ob.open("record.dat",ios::in);
char c;
cout<<"\nContents of file....\n";
while(ob.eof()==0)
{
ob.get(c);
cout<<c;
}
ob.close();
getch();
}
close(ob)
Note: in the code "close" is a file function that closing a file which associated with "ob" object.
SEEKP FUNCTION
// An example of seekp function
// p stands for put and its used to move cursor during writing a file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
void main()
{
clrscr();
char s[40];
ofstream ob;
ob.open("abc.dat",ios::out);
cout<<"Enter any sentence:\n";
gets(s);
ob.seekp(5); // put cursor in fifth position in abc.dat file
ob<<s;
ob.close();
getch();
}
SEEKG FUNCTION
// An example of seekg function
/* g stands for get and its used to get characters from a file
with desired poistion of cursor */
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
void main()
{
clrscr();
char c;
ifstream ob;
ob.open("abc.dat",ios::in);
ob.seekg(8); // get cursor in 8 position in abc.dat file
while(ob) //ob.eof()==0
{
ob.get(c);
cout<<c;
}
ob.close();
getch();
}
0 Comments