




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





File. The information / data stored under a specific name on a storage device, is called a file. Stream. It refers to a sequence of bytes. Text file. It is a file that stores information in ASCII characters. In text files, each line of text is terminated with a special character known as EOL (End of Line) character or delimiter character. When this EOL character is read or written, certain internal translations take place.
Binary file. It is a file that contains information in the same format as it is held in memory. In binary files, no delimiters are used for a line and no translations occur here.
ofstream: Stream class to write on files ifstream: Stream class to read from files fstream: Stream class to both read and write from/to files.
ofstream fout ( “results” ); //output only ifstream fin ( “data” ); //input only OPENING FILE USING open() Stream-object .open( “filename” , mode) ofstream ofile ; ofile .open( “data1” );
ifstream ifile ; ifile .open( “data2” ); File mode parameter Meaning ios::app Append to end of file ios::ate go to end of file on opening ios::binary file open in binary mode ios::in open file for reading only ios::out open file for writing only ios::nocreate open fails if the file does not exist ios::noreplace open fails if the file already exist ios::trunc delete the contents of the file if it exist All these flags can be combined using the bitwise operator OR (|). For example, if we want to open the file example.bin in binary mode to add data we could do it by the following call to member function open():
fstream file ; file .open ( "example.bin" , ios::out | ios::app | ios::binary);
fout .close(); fin .close();
put() and get() function the function put() writes a single character to the associated stream. Similarly, the function get() reads a single character form the associated stream. example : file .get(ch); file .put(ch); write() and read() function write() and read() functions write and read blocks of binary data. example: file .read((char *)& obj , sizeof( obj )); file .write((char *)& obj , sizeof( obj ));
eof() returns true (non zero) if end of file is encountered while reading;otherwise return false(zero)
fail() return true when an input or output operation has failed
bad() returns true if an invalid operation is attempted or anyunrecoverable error has occurred.
good() returns true if no error has occurred.
All i/o streams objects have, at least, one internal stream pointer: ifstream, like istream, has a pointer known as the get pointer that points to the element to be read in the next input operation.
ofstream, like ostream, has a pointer known as the put pointer that points to the location where the next element has to be written.
Finally, fstream, inherits both, the get and the put pointers, from iostream (which is itself derived from both istream and ostream).
These internal stream pointers that point to the reading or writing locations within a stream can be manipulated using the following member functions:
while(!fin.eof()) { fin.get(ch); cout<<ch; } fin.close(); getch(); return 0; }
#include<fstream.h> #include<conio.h> int main() { ifstream fin; fin.open("out.txt"); clrscr(); char ch; int count=0; while(!fin.eof()) { fin.get(ch); count++; } cout<<"Number of characters in file is "<<count; fin.close(); getch(); return 0; }
#include<fstream.h> #include<conio.h> int main() { ifstream fin; fin.open("out.txt"); char word[30]; int count=0; while(!fin.eof()) {
fin>>word; count++; } cout<<"Number of words in file is "<<count; fin.close(); getch(); return 0; }
#include<fstream.h> #include<conio.h> int main() { ifstream fin; fin.open("out.txt"); char str[80]; int count=0; while(!fin.eof()) { fin.getline(str,80); count++; } cout<<"Number of lines in file is "<<count; fin.close(); getch(); return 0; }
#include<fstream.h> int main() { ifstream fin; fin.open("out.txt"); ofstream fout; fout.open("sample.txt"); char ch; while(!fin.eof()) { fin.get(ch);
fp2.write((char*)&obj,sizeof(obj)); fp2.close(); }
void display() { student obj; ifstream fp1; fp1.open("student.dat",ios::binary); while(fp1.read((char*)&obj,sizeof(obj))) { obj.showdata(); } } fp.close(); }
void search (int n) { student obj; ifstream fp1; fp1.open("student.dat",ios::binary); while(fp1.read((char*)&obj,sizeof(obj))) { if(obj.retadmno()==n) obj.showdata(); } fp1.close(); }
void deleterecord(int n) { student obj; ifstream fp1; fp1.open("student.dat",ios::binary); ofstream fp2; fp2.open("Temp.dat",ios::out|ios::binary);
while(fp1.read((char)&obj,sizeof(obj))) { if(obj.retadmno!=n) fp2.write((char)&obj,sizeof(obj)); } fp1.close(); fp2.close(); remove("student.dat"); rename("Temp.dat","student.dat"); }
void modifyrecord(int n) { fstream fp; student obj; int found=0; fp.open("student.dat",ios::in|ios::out); while(fp.read((char)&obj,sizeof(obj)) && found==0) { if(obj.retadmno()==n) { obj.showdata(); cout<<"\nEnter The New Details of student"; obj.getdata(); int pos=-1sizeof(obj); fp.seekp(pos,ios::cur); fp.write((char*)&obj,sizeof(obj)); found=1; } } fp.close(); }