File is a named memory location in secondary storage. File is collection of data stored permanently with in disk. Of stream (output file stream calss) Output file stream class. This class opens the file in output Mode. Always output mode used for writing. Hierarchy of of stream class
osstem (char *fname)
ios::out -this create new file ios:: app-this open existing file for appending.
# include <iostream.h> # include <fstream.h> void Main() { char name[10]; int sub1,sub2; ofstrem fout (“ c:\\Marks”); char ch; clrscr(); cout<<”\n Input name”; cin>>name; cout<<”\n Input two subjects”; cin >>sub1>>sub2; fout<<name<<endl; fout<<sub1<<endl; fout<<sub2<<endl; cout<<”\n Add another student?”; cin >>ch; } while(ch!=’n’); }
# include <iostream.h> #include <conio.h> # include <fstream.h> void main() { char name[10]; int sub1,sub2; ifstream fin clrscr(); while(1) { fin >>name; fin >>sub1>>sub2; if (fin.eof ()) break; cout<<endl<<name<<”\t<<sub1<<”\t”<<sub2; }
# include <iostrem.h> # include<fstream.h> class address { char name[10]; char street[10]; char city[10]; public: void read_address() { cout<<”\n Input name “; cin>>name; cout<<”\n Input street”; cin>> street; cout<<”\n Input city”; cin>> city; } void print_address() { cout<<endl<<name<<”\t”<<street<<”\t”<<city; }}; void main() { address add; char ch; ofstream fout (“c://address_book”); do{ add.read_address() fout.write((char*)&add,size of (add)); cout<<”\n Add another book?”; cin>>ch; } while (ch!=’n’) fout.close(); }
# include <iostream.h> # include<conio.h> #include<fstream.h> class address { char name[10]; char street[10]; char city [10]; public : void read_address() { cout<<”\n Input Name”; cin >>Name; cout<<”\n Input Street”; cin>>street; cout<<”\n Input city”; cin >>city; } void print_address() { cout <<endl<<Name<<”\t”<<street<<”\t”<<city; }}; void main() { address add; ifstream fin(“c:\\address_book”); while(1) { fin .read((char*)&add,sizeof(add)); if (fin,eof()); break; add.print_address(); } fin.close(); }
We can move the file pointer to any desired position in the file. We can take the control of the file pointer. The functions to position the file pointer at desired position. seekg(offset,position);seekp(offset,position); offset parameter represents number of bytes and position parameter takes one of the following 3 constants; ios::beg ios :: cur ios:: end fin.seekg(4,ios::beg); fin.seekg(2, ios::beg); fin.seekg(3,ios::cur); fin.seekg(-4,ios::cur); cout<<fin.tellg(); à fin.seekg(-2,ios::end); => (7-2)=>5 offset position Reading Address randomly: fin.seekg(addno*sizeof(add),ios ::beg); void main() { address add; int addno; ifstream fin(“c:\\address_book”); cout<<”\n Input address number”; cin>>adddno; fin.seekg(addno*sizeof (add),ios::beg); fin read((char*)&add,size of (add)); if fin.eof()) cout<<”\n Invalid address number”; else add print_address(); }
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.