By Sumit Kumar Gupta PGT(CS) KV NO.1, 1st Shift, BBSR
filebuf : It sets the file buffers to read & write. close( ) & open( ) fstreambase : This is the base class for fstream, ifstream, ofstream classes. open( ) close( ) ifstream : input file stram class. It provide input operation for file. It inherit get( ), getline( ), read( ) and function supporting random access (seekg( ) and tellp( )) from istream class defined inside iostream.h ofstream : Output file stram class. It inherit put( ) and write( ) long with function supporting random access (seekp( ) and tellp( )) from ostream class defined class inside iostream.h fstream : it is an I/O file stream class. It inherits all function from istream and ostream classes through iostream class defined inside iostream.h.
Files are required for storing the data and information permanently. 1. Text file : A text file stores information in ASCII characters. Each line terminated by EOL ( end of line). 2. Binary file : It stores information in the same format in which the information is held in memory. No delimiter for line. no translation occurs. It is faster and easier for a program read and write.
Using Constructor: ifstream input_file(“DataFile”); char ch; Input_file>>ch; float amt; input_file>>amt; ofstream output_file(“DataFile”); input_file.close( ); output_file.close( );
Using Function: ifstream fin; fin.open(“data.dat”); fin.close( );
ios::in : open file in read mode, ifstream ios::out : write modeor ios::truncofstream ios::ate : seeks the eof ofstream,ifstream ios::app : appended to the endofstream ios::trunc : open file with zero length ofstream ios::nocreate : not create when open ( ) fail, ofstream ios::noreplace : open( ) fail when file exists, ofstream ios::binary : Open a file in binary mode. By default it is text mode ofstream, ifstream ofstream fout; fout.open(“data.dat”,ios::binary | ios:app); fout.close( );
1. Determine the type of link required. 2. Declare a stream for the desired type of link. 3. Attach the desired file to the stream. 4. Now process as required. 5. Close the file-link with stream.
1. Determine the type of link required. File to memoryinput Memory to Fileoutput Bothinput/output
2.Declare a stream for the desired type of link. File to memoryifstream fin Memory to Fileofstream fout Bothfstream finout
3.Attach the desired file to the stream. ifstream fin(“data.dat”); Or fin.open(“data.dat”, ios::in); ofstream fout(“data.dat”); Or fout.open(“data.dat”, ios::out); fstream finout(“data.dat”); Or finout.open(“data.dat”, ios::in | ios::out);
4.Now process as required. #include void main( ) {ofstream fout; fout.open(“mark.dat”, ios::out); char ans=‘y’; int rollno; while(ans==‘y’ || ans==‘Y’) { cout<<“\nEnter roll no : “; cin>>rollno; fout<<rollno; cout<<“\n Want to enter more? :”; cin>>ans; } fout.close( ); }
5. Close the file-link with stream. fin.close( ); fout.close( );
#include void main( ) {ofstream fout; fout.open(“mark.dat”,ios::out); int rollno; for(int i=0;i<5;i++) { cout<<“\nEnter roll no : “; cin>>rollno; fout<<rollno<<“\n”; } fout.close( ); ifstream fin(“mark.dat”); fin.seekg(0); cout<<“\n”; for(i=0;i<5;i++) {fin.get(rollno); fin>>rollno; cout<<rollno; } }
istream & get (char & ch); ostream & put(char ch); int main( ) { char ch; ifstream fin; ifstream fin(“x.dat”, ios::in); if(!fin) { cout<<“Can’t open”; return 1; } while(fin) { fin.get(ch); cout<<ch; } fin.close( ); return 0; } istream & get(char * buf, int num, char delim=‘\n’) cin.get( line, 40,’$’); istream & getline( char * buf, int num, char delim=‘\n’)
istream & read((char *) &buf, int sizeof(buf)); ostream & write((char *) &buf, int sizeof(buf)); fin.read((char *) &savec, sizeof(customer)); fout.write((char *) &savec, sizeof(customer));
#include struct customer{ char name[51]; float balance;}; void main( ) { customer savac; strcpy(savac.name, “tina”); savac balance= ; ofstream fout; fout.open(“saving”,ios::out | ios::binary); if(!fout) {cout<<“can’t open” ; return 1; } fout.write((char *) &savac, sizeof(customer)); fout.close( ); ifstream fin; fin.open(“saving”,ios::in | ios:: binary); fin.read((char *) &savac, sizeof(customer)); cout<<savac.name<<“ has the balance amount of Rs.”<<savac.balance<<“\n”; fin.close( ); }