DATA FILE HANDLING
Contents to be covered in class Concept of files & streams Stream class hierarchy File handling functions Text V/S Binary Data Files Opening & Closing Files File Modes Some Programming Examples Searching in Binary Files
FILE HANDLING IN C++ Most computer program work with files. This is because files helps in storing Data permanently. A file is a collection of related data stored on a storage device. In C++ file operations make use of streams which provides Interface between programs and the files.
STREAM A Stream is general name given to a flow of data. Different streams are used to represent different kinds of data flow. Each stream is associated with particular Class.
Input Stream The stream that supplies data to the program is known as Input Stream. It reads data from file and hand over data to the program.
Output Stream The stream that receives data from the program is known as output Stream. It writes the received data to the file.
Each stream is associated with a particular class. CLASSPURPOSE ifstreamIt is input file stream, it provides input operations for file. ofstreamIt is output file stream, it provides output operations for file. fstreamIt is input and output file stream. It provides support simultaneous Input and Output operations.
TYPES OF FILES IN C++ TEXT FILES BINARY FILES
TEXT FILES TEXT FILES : A text files stores data in ascii format. In text file each line is terminated with a special character known as EOL character. In text files some Internal translation takes place when this EOL character is read and written.
BINARY FILES BINARY FILES : A binary file stores data in binary format and no delimiters is used for a line. Also no Translation occurs in binary files. As a result, binary files are faster and easier for a program to read and write than the text files.
STEPS TO PROCESS A FILE 1. Determine the type of link required. 2. Declare a stream for the desired types of link. 3. Attach the desired file to the declared the stream. 4. Processing a file. 5. Close the file link with stream.
Opening & Closing Files Two Methods for opening files (i) Using Constructors ( When Single file is to be used ) Eg. ifstream ip_file(“xyz.dat”) – For reading from file ip_file>>ch; ofstream op_file(“pqr.dat”) – For writing to file op_file.put(ch); (ii) Using Open() function (For Multiple files) ifstream fin; fin.open(“abc.dat”); fin.close(); fin.open(“xyz.dat”); --Multiple Files fin.close();
File Modes These are used to access a file as required Eg. Ios::in, ios::out, ios::ate, ios::app, ios::binary etc. Syntax: fout.open(“abc.dat”, ios::in) To concatenate two or more modes: ios::app|ios::nocreate|ios::binary)
iostream.h file fstream.h file IOS ISTREAM STREAMBUF OSTREAM IFSTREAM IOSTREAM FSTREAM OFSTREAM FSTREAMBASE FILEBUF Stream Class Hierarchy
/* program to write data in a text file */ #include void main() { char ans='y'; char name[20],add[20]; ofstream fout("student.dat"); clrscr(); while(ans=='y') { cout<<"Enter name and address:"<<endl; cin>>name>>add; fout<<name<<add<<endl; cout<<"Do you want to add another record:"; cin>>ans; } fout.close(); }
/* program to read data from a file */ #include void main() { char name[20],add[20]; ifstream fin("student.dat"); clrscr(); while(1) { fin>>name>>add; cout<<name<<add<<endl; if(fin.eof()) break; } fin.close(); }
/* program to write and read data from a file */ #include void main() { char string[80],ch; int len; fstream file; file.open(“string.dat”,ios::out| ios::in); clrscr(); cout<<“Please enter the String:”; gets(string); len=strlen(string); // logic to write data in a file for(int i=0,i<=len-1;i++) { file.put(string[i]); }
// logic to read data from a file file.seekg(0); while(1) { file.get(ch); if(file.eof()) break; cout<<ch; } file.close(); getch(); }
The read() & write() functions using structures #include struct customer {char name[]; float balance; }; void main() { clrscr(); customer c1; strcpy(c1.name, “Ajay Jain”); c1.balance= ; ofstream fout; fout.open(“account”,ios::out| ios::binary); if(!fout) { cout<<“\n File can’t be open”; return 1;} fout.write((char *) & c1, sizeof(customer)); fout.close(); ifstream fin; fin.open(“account”,ios::out| ios::binary); fin.read((char *) & c1, sizeof(customer)); cout<<c1.name<<“ has the balance amount of Rs. ”<< c1.balance; fout.close(); }
Reading & writing class objects #include class Student {char name[10]; float marks; public: void getdata() {char ch; cin.get(ch); cout<<“\n Enter name: ”; cin.getline(name, 40); cout >marks; cout<<endl; } void display() { cout<< name <<“\t”<<marks<<“\n”; } };
void main() { clrscr(); Student S[2]; fstream file; file.open(“student.dat”,ios::in| ios::out); if(!file) { cout<<“\n File can’t be open”; return 1;} cout<<“\n Enter details of students \n ”; for(int i=0;i<2;i++) {s[i].getdata(); file.write((char *) & S1[i], sizeof(S[i])); } file.seekg(); cout<<“\n The contents of student.dat are : \n”; for(int i=0;i<2;i++) {file.read((char *) & S1[i], sizeof(S[i])); s[i].display(); } fout.close(); }
THANKS & HAVE A NICE DAY