>ch; cout< >ch; cout<

Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream.

Similar presentations


Presentation on theme: "File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream."— Presentation transcript:

1 File Handling

2 Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream fin("in.txt"); //file will be searched in current directory char ch; while(fin.eof() != 0) { fin>>ch; cout<<ch; } return 0; }

3 Read data from file and display it on screen #include int main() { ifstream fin(“c:\\in.txt"); //give full path of file char ch; while(fin.eof() != 0) { fin>>ch; cout<<ch; } return 0; }

4 Read data from file and display it on screen #include int main() { /* fin is created with default constructor so use open() to open file */ ifstream fin; fin.open(“in.txt”); char ch; while(fin.eof() != 0) { fin>>ch; cout<<ch; } return 0; }

5 Read data from one file and save it on two different file depending on condition #include int main() { ofstream fout1,fout2; fout1.open("alpha.txt"); fout2.open("digits.txt"); ifstream fin; fin.open("in.txt"); char ch; 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; }

6 Take data from user store it in file again read data from file and display it on screen. #include 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(); 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; }

7 Use of getline function #include int main() { ifstream fin("item.txt"); char line[80]; while(fin) { fin.getline(line,80); cout<<endl<<line; } return 0; }

8 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();

9 Example of seekg() 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"; /*moves get pointer to 1 st location*/ f.seekg(0); f>>name; f>>cost; cout<<"\n Item name :"; cout<<name; cout<<"\n Cost :"; cout<<cost; return 0; }

10 Example of seekg() 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"; /*moves get pointer to 3 rd location*/ f.seekg(3); f>>name; f>>cost; cout<<"\n Item name :"; cout<<name; cout<<"\n Cost :"; cout<<cost; return 0; }

11 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::cur cureent position of the pointer ios::end end of the file

12 Pointer offset calls Seek callsAction fout.seekg(0,ios::beg)Go to start Fout.seekg(0,ios::cur)Stay at the current position Fout.seekg(0,ios::end)Go to end of the file fout,.seekg(m,ios::beg)Moves to (m+1) th byte in the file Fout.seekg(m,ios::cur)Go forward by m bytes from current posiotion Fout.seekg(-m,ios::cur)Go backward by m bytes from the current position Fout.seekg(-m,ios::end)Go backward by m bytes from end of the file

13 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))

14 Example #include class item { char name[20]; float cost; public: void readdata(); void writedata(); }; void item::readdata() { cout >name; cout >cost; } void item::writedata() { cout<<setiosflags(ios::left)<<setw(10)<<name; cout<<endl<<setprecision(2)<<cost; } int main() { item ob[3]; fstream f; f.open("stock.dat",ios::in | ios::out); cout<<"\n enter details for items"; for(int i=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; }

15 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.

16 Command line Argument


Download ppt "File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream."

Similar presentations


Ads by Google