M5 Filehandling
M5 Filehandling
Files
• Many real-life problems handle large volumes
of data. The data is stored in the devices using the
concept of files.
• A file is a collection of related data stored in
a particular area on the disk.
• A computer file is a computer resource for
recording data discretely in a computer storage
device.
• Programs are designed to perform read and
write operations on these files.
Console-Program-File interaction
Program File Communication
ios
file m
a
tre
iostream
fstream base
Opening and Closing a File
• To open a file, a file stream is created and then it is
linked to the filename.
• A file can be opened in two ways:
Output:
Enter item
Opening Files Using open()
• The function open() can be used to
open multiple files that use the same stream object.
• Syntax:
file-stream-class stream-
object; stream-
object.open(“filename”);
• A stream object can be connected to only one file at a
time.
Opening Files Using open()
const int N=80;
char line[N];
#include<iostream.h> ifstream fin;
#include<fstream.h> fin.open(“Country”);
cout<<“Contents of
int main()
country file” ;
{
ofstream fout; while(fin)
fout.open(“Country”); {
fin.getline(lin
fout<<“United state of e, N);
America\n”; cout<<line;
fout<<“United }
Kingdom\n”; fin.close();
fin.open(“Capi
fout.close(); tal”);
cout<<“Conte
fout.open(“Capital”); nts of capital
file”;
fout<<“Washington\n” while(fin)
fout<<“London”; {
fin.ge
fout.close();
tline(l
Detecting End-of File
• Detection of the end-of-file condition is necessary for preventing any
further attempt to read data from the file.
while(fin)
• An ifstream object return a value zero if any error occurs in the file
operation including the end-of-file condition.
if(fin1.eof() != 0 ) { exit(1); }
• The eof() of ios class returns a non zero value if the end-of-file
condition is encountered and zero otherwise.
Opening two files simultaneously
• Syntax:
▫ infile.read ((char *) & V, sizeof (V));
▫ outfile.write ((char *) & V, sizeof (V));
• The first argument is the address of the variable V.
• The second is the length of that variable in bytes.
• The address of the variable must type cast to char *
(ie: pointer to character type).
Example
#include<iostream> void inventory :: readdata(void)
{
#include<fstream> cout<<“Enter name:”;
using namespace std; cin>> name;
cout<<“Enter code:”;
class inventory cin>>code;
{ cout<<“Enter cost:”;
cin>> cost;
char name[10]; }
int code; void inventory :: writedata(void)
{
float cost;
cout<<name;
public:
cout<<code;
void readdata(void); cout<<cost;
void writedata(void); }
};
int main()
{
inventory item[3];
fstream file;
file.open(“Stock.dat”, ios::in | ios::out);
cout<<“Enter the details for three items:”;
for(int i=0; i<3;i++)
{
item[i].readdata();
file.write((char *) & item[i], sizeof(item[i]));
}
file.seekg(0);
for(i=0;i<3;i++)
{
file.read((char *) & item[i], sizeof(item[i]));
item[i].writedata();
}
file.close();
return 0;
}
Updating a File: Random Access
(termin
ate
progra
m
normall
y)
} infile.clear ( ); // clear error state
else
.........
if(infile.
} bad ( ))
{ ..........
.......... ..........