170 likes | 345 Views
File Handling. Read data from file and display it on screen. #include< iostream.h > #include< fstream.h > int main() { /* fin object is created with parameterzied constructor */ ifstream fin("in.txt "); //file will be searched in current directory char ch ;
E N D
Read data from file and display it on screen #include<iostream.h> #include<fstream.h> int main() { /* fin object is created with parameterzied constructor */ ifstreamfin("in.txt"); //file will be searched in current directory char ch; while(fin.eof() != 0) { fin>>ch; cout<<ch; } return 0; }
Read data from file and display it on screen #include<iostream.h> #include<fstream.h> int main() { ifstreamfin(“c:\\in.txt"); //give full path of file char ch; while(fin.eof() != 0) { fin>>ch; cout<<ch; } return 0; }
Read data from file and display it on screen #include<iostream.h> #include<fstream.h> int main() { /* fin is created with default constructor so use open() to open file */ ifstreamfin; fin.open(“in.txt”); char ch; while(fin.eof() != 0) { fin>>ch; cout<<ch; } return 0; }
Read data from one file and save it on two different file depending on condition while(fin) { fin>>ch; if(ch >=65 && ch <=124) fout1<<ch; else if(ch >=48 && ch <=57) fout2<<ch; cout<<ch; } fin.close(); fout1.close(); fout2.close(); return 0; } #include<iostream.h> #include<fstream.h> int main() { ofstream fout1,fout2; fout1.open("alpha.txt"); fout2.open("digits.txt"); ifstream fin; fin.open("in.txt"); char ch;
Take data from user store it in file again read data from file and display it on screen. f.open("item.txt",ios::in); f>>name; f>>cost; cout<<"\n Item name :"; cout<<name; cout<<"\n Cost :"; cout<<cost; f.close(); return 0; } #include<iostream.h> #include<fstream.h> int main() { fstream f; f.open("item.txt",ios::out); char name[20]; float cost; cout<<"\n Enter item name"; cin>>name; f<<name<<"\n"; cout<<"\n Enter cost"; cin>>cost; f<<cost<<"\n"; f.close();
Use of getline function #include<iostream.h> #include<fstream.h> int main() { ifstream fin("item.txt"); char line[80]; while(fin) { fin.getline(line,80); cout<<endl<<line; } return 0; }
Functions for manipulation of file pointers • seekg() Moves get pointer to a specified location • seekp() Moves put pointer to a specified location. • tellg() Gives the current position of the get pointer • tellp() Gives the current position of the put pointer • E.g. fin.seekg(10); int p = fout.tellp();
Example of seekg() /*moves get pointer to 1st location*/ f.seekg(0); f>>name; f>>cost; cout<<"\n Item name :"; cout<<name; cout<<"\n Cost :"; cout<<cost; return 0; } int main() { fstream f; f.open("item.txt",ios::in | ios::out); char name[20]; float cost; cout<<"\n Enter item name"; cin>>name; f<<name<<"\n"; cout<<"\n Enter cost"; cin>>cost; f<<cost<<"\n";
Example of seekg() /*moves get pointer to 3rdlocation*/ f.seekg(3); f>>name; f>>cost; cout<<"\n Item name :"; cout<<name; cout<<"\n Cost :"; cout<<cost; return 0; } int main() { fstream f; f.open("item.txt",ios::in | ios::out); char name[20]; float cost; cout<<"\n Enter item name"; cin>>name; f<<name<<"\n"; cout<<"\n Enter cost"; cin>>cost; f<<cost<<"\n";
Functions with offset • seekg(offset,refposition) • Seekp(offset,refposition) • Offset represents number of bytes the file pointer is to be moved from the location specified by the parameter refposiotion. • Refposition takes one of the form specified in ios class (constant) • ios::beg beginning of file • ios::curcureent position of the pointer • ios::end end of the file
Reading and Writing Class object • read() is used to read object data from file • Syntax read( (char *) &obj_name , sizeof(obj_name)) • write() is used to write object data to the file • Syntax write((char *) &obj_name, sizeof(obj_name))
Example int main() { item ob[3]; fstream f; f.open("stock.dat",ios::in | ios::out); cout<<"\n enter details for items"; for(inti=0;i<3;i++) { ob[i].readdata(); f.write( (char *) &ob[i],sizeof(ob[i])); } cout<<"\n Output : "; for(i=0;i<3;i++) { f.read((char*) &ob[i] , sizeof(ob[i])); ob[i].writedata(); } return 0; } #include<iostream.h> #include<fstream.h> #include<iomanip.h> class item { char name[20]; float cost; public: void readdata(); void writedata(); }; void item::readdata() { cout<<"\n Enter item name: "; cin>>name; cout<<"\n Enter item Cose: "; cin>>cost; } void item::writedata() { cout<<setiosflags(ios::left)<<setw(10)<<name; cout<<endl<<setprecision(2)<<cost; }
Error handling during file operations • eof() Returns true if end of file encountered • fail() Returns true when input or output operation has failed • bad() Returns true invalid option is attempted(used for fatal errors) • good() Returns true if no error has occurred .