File concepts in C++

  • Share this blog:

Files in C++

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 Screenshot_35  

File Constructor

osstem (char *fname)

  • This constructor create osstream class object with given file name.
  • ostream(char*fname,int mode)
  • This constructor create ostrem class object with give filename and mode.

ios::out -this create new file ios:: app-this open existing file for appending.  

//program to create marks file to store student marks details

# 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’); }    

ifstream (input file stream class in C++)          

  • input file stream class
  • This class is used to read data from existing file.
  • it opens the file in input mode(reading the file).



  • ifstream (char *fname)
  • This constructor create if stream class object with given filename.
  • It opens the given file name in input mode.


Program to read contents of file in C++

# 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; }  

 Writing Object inside file

Screenshot_36     # 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(); }    

//Reading address from address book/reading objects from file

# 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 Screenshot_37 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

Recommended Courses


About Author
Author Bio

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.

Stay Updated

Get stories of change makers and innovators from the startup ecosystem in your inbox