Data Files
Data Files
INTRODUCTION
- IN COMPUTER THE PROCESSING TAKES PLACE BY HAVING INPUTS
FROM THE INPUT DEVICES AND AFTER PROCESSING SENDING THE
OUTPUT TO THE OUTPUT DEVICES. WHILE THE PROCESSING IS
TAKING PLACE AND ALSO WHEN THE PROCESSING IS OVER, THE
MEMORY IS USED FOR STORING ALL THE DATA OF THE SPECIFIED
TYPE. IN THIS KIND OF OUPUT THE STORAGE OF DATA IS NOT
PERMANENT. WHEN THE COMPUTER IS SWITCHED OFF THE DATA IN
MEMORY ALSO GETS ERASED OFF.
- THE FILES IN C++ ALLOWS TO STORE THE DATA PERMANENTLY SO
THAT THE PREVIOUSLY ENTERED DATA IS AVAILABLE TO THE
PROGRAM.
- A FILE IS A COLLECTION OF BYTES STORED ON SOME STORAGE
DEVICE LIKE DISK.
STREAMS
- A STREAM IS A SEQUENCE OF BYTES.
- IN C++ DATA FLOWS FROM THE SOURCE TO THE SINK
(DESTINATION).
- THE FLOW OF DATA FROM SOURCE TO DESTINATION ( SINK) IS
CALLED A STREAM.
- cin AND cout USED FOR INPUT AND OUTPUT ARE ALSO KNOWN AS
STEAMS.
- IN STREAM cin DATA FLOWS FROM THE KEYBOARD TO THE RAM.
- IN STREAM cout DATA FLOWS FROM THE RAM TO THE MONITOR. –
- STREAM cin IS THE MEMBER OF CLASS istream. STREAM cout IS THE
MEMBER OF CLASS ostream.THESE CLASSES ARE DECLARED IN
HEADER FILE iostream.h. OBJECTS cin AND cout HAVE ALSO BEEN
DECLARED IN THE SAME FILE. AS OBJECTS cin AND cout HAVE
ALREADY BEEN DECLARED, YOU CAN USE THEM ONLY BY
INCLUDING HEADER FILE iostream.h IN THE PROGRAM
- THE OPERATION OF WRITING THE DATA TO FILE INVOLVES FLOW
OF DATA FROM THE RAM TO THE FILE AND THE OPERATION OF
READING THE DATA FROM THE FILE INVOLVE FLOW OF DATA FROM
THE FILE TO THE RAM.
READ INPUT
DATA STREAM DATA
DISK
FILE PRGRAM
ios
iostream
fstreambase
WRITEING TO TEXTFILE
- OFSTREAM IS USED FOR OPENING THE FILE FOR WRITING PURPOSE.
PROGRAM:
//example of writing to a text file using the constructor to stream class
#include<fstream.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int ch;
ofstream fout("chars.dat");
if(fout==NULL)
{
cout<<"\nerror in opening file";
exit(1);
}
for(ch='A';ch<='Z';ch++)
{
fout<<ch<<" ";
}
fout<<endl;
fout.close();
}
PROGRAM
// example of writing to a text file using the open() function
#include<fstream.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int ch;
ofstream fout;
fout.open("chars.dat");
if(fout==NULL)
{
cout<<"\nerror in opening file";
exit(1);
}
for(ch='A';ch<='Z';ch++)
{
fout<<ch<<" ";
}
fout<<endl;
fout.close();
}
//program to input the roll number and marks of a set of students and
//store the data in the file
#include<fstream.h>
#include<conio.h>
#include<iomanip.h>
ofstream fout("result.dat");
class student
{
private:
int rollno,marks;
public:
void getdata()
{
cout<<"\n enter roll number";
cin>>rollno;
cout<<"\n enter marks";
cin>>marks;
}
void tofile()
{
fout<<setw(10)<<rollno<<setw(10)<<marks<<endl;
}
};
void main()
{
int i,n;
student stud;
clrscr();
cout<<"\nenter number of students";
cin>>n;
fout<<setw(10)<<"rollno"<<setw(10)<<"marks"<<endl;
for(i=0;i<n;i++)
{
stud.getdata();
stud.tofile();
}
fout.close();
}
INPUT FUNCTIONS
GET()
- FUNCTION GET() READS A SINGLE CHARACTER FROM AN INPUT
STREAM.
PROGRAM.
//program to copy a source file to destination file
#include<fstream.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
char infile[5];
char outfile[5];
char ch;
clrscr();
while(!fin.eof())
{
ch=fin.get();
fout<<ch;
} ofstream fout("afile");
for(int i=0;i<10;i++)
{
cout<<"\n enter char in the file";
cin>>ch;
fout<<ch;
}
fout.close();
ifstream fin("afile");
if(!fin)
{
cout<<"\n error opening file";
exit(1);
}
ofstream fou("bfile");
while(!fin.eof())
{
ch=fin.get();
fou<<ch;
}
fin.close();
fou.close();
cout<<"\n copied data in new file";
ifstream fi("bfile");
while(!fi.eof())
{
ch=fi.get();
cout<<ch;
}
fi.close();
}
GETLINE()
- THE EXTRACTION OPERATOR (>>) IS OVERLOADED TO READ ANY
VARIABLE, CHARACTER, INTEGER,FLOAT OR DOUBLE.
- IT CAN READ A CHARACTER STRING ONLY IF THERE IS NO BLANK
SPACE IN IT. BECAUSE OPERATOR(>>) TAKES BLANK SPACE AS THE
DELIMITER WHICH SEPARATES TWO VARIABLES.
PROGRAM
//program to read line of text from the keyboard and
// write the same to a file
#include<fstream.h>
#include<conio.h>
void main()
{
ofstream fout;
char line[80];
fout.open("fname");
clrscr();
cout<<"\n enter line of text";
cin.getline(line,80);
fout<<line;
fout.close();
char ch;
cout<<"\n data entered in the file is\n";
ifstream fin;
fin.open("fname");
while(!fin.eof())
{
fin.get(ch);
cout<<ch;
}
fin.close();
}
PUT()
- FUNCTION PUT() WRITES A CHARACTER TO A STREAM.
- THE FUNCTION TAKES A SINGLE CHARACTER TO THE STREAM.
EXAMPLE.
Ofstream fout.put(‘*’);
Will put * in the file to which stream fout is linked.
#include<fstream.h>
#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
ofstream fout("chars");
clrscr();
for(ch='A';ch<='Z';ch++)
{
cout.put(ch);
fout<<ch;
}
fout.close();
ifstream fin("chars");
cout<<"\n characters in file are";
while(!fin.eof())
{
fin.get(ch);
cout<<ch;
}
fin.close();
}
WRITE()
- OVERLOADED STREAM INSERTION FUNCTION AND FUNCTIONS LIKE
PUT() WRITE DATA TO THE TEXT FILES IN WHICH NUMERIC DATA
LIKE INTEGER AND REAL NUMBERS ARE FIRST TRANSLATED INTO
CHARACTER STRINGS BEFORE THEY ARE WRITTEN TO FILES.
- IN BINARY FILES THE NUMBERS ARE WRITTEN AS THEY ARE
STORED IN THE RAM.
- WRITE() FUNCTION WRITES DATA TO THE BINARY FILE.
- FUNCTION WRITE() ALSO ALLOWS TO WRITE THE ENTIRE
STRUCTURE OR OBJECT OF THE CLASS IN THE FILE.
SYNTAX:
streamobject.write((char *)&objectname,sizeof(object));
-Here sizeof(object) finds out the total no. of bytes to be written
&objectname returns the reference of object name starting from where the specified
no. of bytes are to be written.
(char *)&objectname casts the reference of objectname to char type pointer before it’s
written on to the file.
fout.write((char *)&stud, sizeof(stud));
READ()
- FUNCTION read() CAN READ DATA FROM A BINARY FILE.
- IT CAN ALSO READ BLOCK OF DATA. A SINGLE READ STATEMENT
CAN READ ALL THE DATA MEMBERS OF A STRUCTURE VARIABLE
OR AN OBJECT.
SYNTAX
streamname.read((char *)&objectname,sizeof(object));
Here sizeof(object) finds out the total no. of bytes to be read.
&objectname returns the reference of object name starting from where the specified
no. of bytes are to be read.
(char *)&objectname casts the reference of objectname to char type pointer before it’s
read from the file.
fout.read((char *)&stud, sizeof(stud));
Program
#include<fstream.h>
#include<conio.h>
#include<iomanip.h>
class item
{
public:
int no;
char name[10];
int rate, qty;
void getdata();
void putdata();
int cal();
};
void item::getdata()
{
cout<<"\n enter item code";
cin>>no;
cout<<"\n enter item name";
cin>>name;
cout<<"\n enter rate";
cin>>rate;
cout<<"\n enter quantity";
cin>>qty;
}
void item::putdata()
{
int amount;
cout<<setw(5)<<"\n item code";
cout<<setw(10)<<"item name";
cout<<setw(10)<<"rate";
cout<<setw(10)<<"quantity";
cout<<setw(10)<<"amount";
cout<<endl;
cout<<setw(10)<<no;
cout<<setw(10)<<name;
cout<<setw(10)<<rate;
cout<<setw(10)<<qty;
amount =cal();
cout<<setw(10)<<amount;
int item::cal()
{
int t=rate * qty;
return t;
}
void main()
{
item it;
fstream fout;
clrscr();
fout.open("item.dat",ios::binary|ios::out);
char ans='y';
while(ans=='y')
{
it.getdata();
fout.write((char *)&it,sizeof(it));
cout<<"\n wish to enter more data";
cin>>ans;
}
fout.close();
fout.open("item.dat",ios::binary|ios::in);
int count=0;
while(!fout.eof())
{
fout.read((char *)&it,sizeof(it));
count++;
}
fout.close();
fout.open("item.dat",ios::binary|ios::in);
cout<<"no. of records"<<count-1;
for(int i=0;i<count-1;i++)
{
fout.read((char *)&it,sizeof(it));
it.putdata();
}
fout.close();
}
FUNCTION seep()
FUNCTION seekg() POSITIONS THE FILE FOR READING DATA FROM IT,
FUNCTION seetp() POSITIONS FILE FOR WRITING TO IT.
ERROR HANDLING
- SOMETIMES DURING FILE OPERATIONS, ERRORS MAY APPEAR.
- TO CHECK FOR ERRORS AND TO ENSURE SMOOTH PROCESSING, C++
FILE STREAMS INHERIT “STREAM STATE” MEMBERS FROM THE ios
CLASS THAT STORE THE INFORMATION ON THE STATUS OF A FILE
THAT IS BEING CURRENTLY USED.
TABLE OF FUNTIONS WITH THEIR MEANINGS
FUNCTION MEANING
Int bad() Returns non-zero value if an invalid operation is attempted or any
unrecoverable error has occurred. Otherwise returns zero value and
it is possible to recover from any other error reported and continue
operation.
int eof() returns non-zero if end of file is encountered while reading
otherwise returns zero.
int fail() returns nonzero when an input or ouput operation has failed.
int good() retruns non-zero if no error has occurred. this means all the above
functions are false. when it returns zero no further operations can
be carried on.
clear() resets the error state so that further operations can be attempted.
Few examples of using above mentioned functions:
ifstream fin;
fin.open(“master”);
while(!fin.fail())
{
cout<<”it’s okey”;
}
if(fin.eof())
{
exit(1);
}
else if(fin.bad())
{
cout<<”fatal error”;
else
fin.clear();