Académique Documents
Professionnel Documents
Culture Documents
A stream is an abstraction that represents a device on which input and ouput operations are performed. A stream can basically be represented as a source or destination of characters of indefinite length. Streams are generally associated to a physical source or destination of characters, like a disk file, the keyboard, or the console, so the characters gotten or written to/from our abstraction called stream are physically input/output to the physical device. For example, file streams are C++ objects to manipulate and interact with files; Once a file stream is used to open a file, any input or output operation performed on that stream is physically reflected in the file.
The mother of all base classes is ios. (In more recent compilers, ios has been replaced by ios_base.) The class ios contains most of the actual I/O code. It is ios that keeps track of the error state of the stream. The error ags are an enumerated type within ios. In addition the ios class converts data for display. It understands the format of the di_erent types of numbers, how to output character strings, and how to convert an ASCII string to and from an integer or a oating{point number. Standard output, cout, is an object of the class ostream, as is cerr, the standard error output. Standard input, cin, is an object of the class istream. cout, cin, and cerr are automatically constructed as global objects at program start{up. Objects of iostream deal with both input and output. Objects of ifstream deal with input _les and objects of the class ofstream deal with output _les. Objects fstream deal with _les that can one can write to and read from. ofstream, ifstream, and fstream are subclasses that are de_ned in the include _le fstream.h. Notice that fstream.h deals
Program Files
Data Files
Text Files
Binary Files
The Language like C/C++ treat every thing as a file , these languages treat keyboard , mouse, printer, Hard disk , Floppy disk and all other hardware as a file. The Basic operation on text/binary files are : Reading/writing ,reading and manipulation of data stored on these files. Both types of files needs to be open and close. Using member function Open( ) Syntax Filestream object; Object.open(filename,mode); Example ifstream fin; fin.open(abc.txt) How to open File Using Constructor Syntax Filestream object(filename,mode); Example ifstream fin(abc.txt);
NOTE: (a) Mode are optional and given at the end . (a) Filename must follow the convention of 8.3 and its extension can be anyone
Program file
SCR
Abc.txt
Program
Program
#include<fstream> using namespace std; int main() { ofstream fout; fout.open("abc.txt"); fout<<"This is my first program in file handling"; fout<<"\n Hello again"; fout.close(); return 0; }
ABC.TXT
#include<fstream> #include<iostream> #include<conio.h> using namespace std; int main() { ifstream fin; char str[80]; fin.open("abc.txt"); fin>>str; // read only first //string from file cout<<"\n From File :"<<str; // as //spaces is treated as termination point getch(); return 0; }
Example : To read the contents of a text file and display them on the screen. Program ( using getline member function) Program ( using get( ) member function)
#include<fstream> #include<conio.h> #include<iostream> using namespace std; int main() { char str[100]; ifstream fin; fin.open("c:\\abc.txt"); while(!fin.eof()) { fin.getline(str,99); cout<<str; } fin.close(); getch(); return 0; } #include<fstream> #include<conio.h> #include<iostream> using namespace std; int main() { char ch; ifstream fin; fin.open("file6.cpp"); while(!fin.eof()) { fin.get(ch); cout<<ch; } fin.close(); getch(); return 0; }
Writing/Reading Data in Binary Format To write and read data in binary format two member functions are available in C++. They are read( ) and write( ). Syntax for Write( ) member function Fileobject.write( (char *)&object, sizeof(object)); D.A.V. Centenary Public School Chander Nagar, Ghaziabad
} To Read data from a binary File using read( ) member function Program using read( ) member function output
#include<fstream> #include<iostream> #include<conio.h> using namespace std; struct student { int roll ; char name[30]; char address[60]; }; int main() { student s; ifstream fin; fin.open("student.dat"); fin.read((char *)&s,sizeof(student)); cout<<"\n Roll Number :"<<s.roll; cout<<"\n Name :"<<s.name; cout<<"\n Address :"<<s.address; fin.close(); getch(); return 0; }
using namespace std; class student { int roll ; char name[30]; char address[60]; public: void read_data( ); void write_data( );
}; void student::read_data( ) // member function defintion { cout<<"\n Enter Roll :"; cin>>roll; cout<<"\n Student name :"; cin>>name; cout<<"\n Enter Address :"; cin>>address; } void student:: write_data() { cout<<"\n Roll :"<<roll; cout<<"\n Name :"<<name; cout<<"\n Address :"<<address; } int main() { student s; ofstream fout; fout.open("student.dat"); s.read_data(); // member function call to get data from KBD fout.write((char *)&s,sizeof(student)); // write object in file fout.close(); return 0; }
}; void student::read_data( ) // member function defintion { cout<<"\n Enter Roll :"; cin>>roll; cout<<"\n Student name :"; cin>>name; cout<<"\n Enter Address :"; cin>>address;
} void student:: write_data() { cout<<"\n Roll :"<<roll; cout<<"\n Name :"<<name; cout<<"\n Address :"<<address; } int main() { student s; ifstream fin; fin.open("student.dat"); fin.read((char *)&s,sizeof(student)); s.write_data(); fin.close(); getch(); return 0; }
seekp( )
tellp( )
tellg( )
Files MODES
D.A.V. Centenary Public School Chander Nagar, Ghaziabad
File mode ios::in ios::out ios::binary ios::app ios::ate ios::nodelete ios::noreplace ios::nocreate
Explanation Input mode Default mode with ifstream and files can be read only Output mode- Default with ofstream and files can be write only Open file as binary Preserve previous contents and write data at the end ( move forward only) Preserve previous contents and write data at the end.( can move forward and backward ) Do not delete existing file Do not replace file Do not create file
NOTE : To add more than one mode in a file stream use bitwise OR ( | ) operator Example :
get( ) member function Get() function is used to read a single char from the input stream in text file Syntax fileobject.get(char); Example: Fin.get(ch); //fin is file stream.
getline() function Getline() function is used to read a string from the input stream in text file. Syntax fileobject.getline (string, no_of_char, delimiter ); Example fin.getline(str,80); // fin is file stream. NOTE: Delimiter is optional
#include<iostream> #include<fstream> using namespace std; int main() { ofstream ob1("f1.txt"); char ch; while((ch=cin.get())!='$') { ob1.put(ch); } ob1.close(); ifstream ib1("f1.txt"); while(!ib1.eof()) { ib1>>ch; cout<<ch<<"\t"; } D.A.V. Centenary Public School Chander Nagar, Ghaziabad
ib1.close(); cin.get(); cin.get(); } Ex2: 2) copy content one file another file. #include<iostream> #include<fstream> using namespace std; int main() { ifstream ob1("f1.txt"); ofstream ob2("f2.txt"); char ch; while(ob1>>ch) { ob2.put(ch); } ob1.close(); ob2.close(); cin.get(); cin.get(); } 3) #include<iostream> #include<fstream> using namespace std; int main() { ofstream ob1("f1.txt"); char a='b',b='t',c='u'; ob1<<a<<b<<c; ob1.seekp(0,ios_base::end); int x=ob1.tellp(); cout<<x<<endl; ob1.close(); cin.get(); } 4) #include<iostream> #include<fstream> using namespace std; int main() { fstream ob1("f1.txt"); char ch; D.A.V. Centenary Public School Chander Nagar, Ghaziabad
ch='o'; ob1<<ch; ch=' '; ob1<<ch; ch='o'; ob1<<ch; char g1,g2,g3; ob1.seekp(0,ios_base::beg); ob1>>g1>>g2>>g3; cout<<g1<<"\t"<<g2<<"\t"<<g3<<endl; cout.flush(); ob1.close(); cin.get(); } Text File Operations #ifndef BOOKS_H #define BOOKS_H #include<iostream> #include<fstream> using namespace std; class Book { int isbn; string title; double price; public: Book(int a,string b,double c):isbn(a),title(b),price(c){} Book(){} void disp() { cout<<isbn<<"\t"<<title<<"\t"<<price<<endl; } void readFromFile() { ifstream ib1("D://f1.txt"); while(ib1>>isbn>>title>>price) { disp(); } ib1.close(); } void copy(char src[20],char dest[20]) { ofstream ob1(dest,ios::app); ifstream ib1(src); D.A.V. Centenary Public School Chander Nagar, Ghaziabad
while(ib1>>isbn>>title>>price) { ob1<<isbn<<"\t"<<title<<"\t"<<price<<endl; } ib1.close(); ob1.close(); } void copy1(char src[20],char dest[20]) { ofstream ob1(dest,ios::app); ifstream ib1(src); char ch; while(!ib1.eof()) { ch=ib1.get(); ob1.put(ch); } ib1.close(); ob1.close(); } void delete1(int n) { Book b1[10];int i=0; ifstream ib1("D://f1.txt"); while(ib1>>b1[i].isbn>>b1[i].title>>b1[i].price) { i++; } ib1.close(); ofstream ob1("D://f2222.txt"); for(int k=(i-1);k<i;k++) { b1[k]=b1[k+1]; } for(int j=0;j<(i-1);j++) ob1<<b1[j].isbn<<"\t"<<b1[j].title<<"\t"<<b1[j].price<<endl; ob1.close(); } void update(int n) { Book b1[10];int i=0; ifstream ib1("D://f1.txt"); while(ib1>>b1[i].isbn>>b1[i].title>>b1[i].price) { i++; } ib1.close(); ofstream ob1("D://f21.txt"); b1[n].isbn=999; b1[n].title="ooop"; D.A.V. Centenary Public School Chander Nagar, Ghaziabad
#include<iostream> using namespace std; #include "Books.h" int main() { Book b1; b1.readFromFile(); b1.copy("D://f1.txt","D://f22.txt"); b1.copy1("D://f1.txt","D://f222.txt"); b1.update(1); b1.delete1(1); cin.get(); } Ex2: Binary Files: #include <iostream> #include <fstream> #include<string.h> using namespace std; class student { int usn; char name[10]; public : student(){} student (int a,char b[10]) { usn=a; strcpy(name,b); } void write(student s[5]) { ofstream ob1("D://bf1.dat",ios_base::binary); ob1.write((char*)s,sizeof(student)*4); ob1.close(); } void disp(student s[5]) { for(int i=0;i<4;i++) D.A.V. Centenary Public School Chander Nagar, Ghaziabad
cout<<s[i].usn<<"\t"<<s[i].name<<endl; } void read() { student s[5]; ifstream ib1("D://bf1.dat",ios_base::binary); ib1.read((char*)s,sizeof(student)*4); disp(s); ib1.close(); } void update(int p) { student temp; ofstream ob1("D://bf1.dat",ios_base::binary | ios_base::in | ios_base::out); //ob1.seekp(0,ios_base::beg); ob1.seekp((p-1)*sizeof(student),ios_base::beg); //temp.usn=786; strcpy(temp.name,"God"); usn=786; strcpy(name,"God"); cout<<ob1.tellp()<<endl; //ob1.write((char*)&temp,sizeof(student)); ob1.write((char*)this,sizeof(student)); ob1.close(); } void delete1(int p) { student temp; ofstream ob1("D://bf1.dat",ios_base::binary | ios_base::in | ios_base::out); ob1.seekp(0,ios_base::beg); ob1.seekp((p-1)*sizeof(student),ios_base::beg); //temp.usn=786; strcpy(temp.name,"God"); usn=786; strcpy(name,"God"); cout<<ob1.tellp()<<endl; //ob1.write((char*)&temp,sizeof(student)); ob1.write((char*)this,sizeof(student)); ob1.close(); } void readWrite() { student temp[5]; fstream ob("D://bf1.dat",ios_base::binary| ios_base::in | ios_base::out); ob.seekp(sizeof(student),ios_base::beg); usn=999; strcpy(name,"oops"); ob.write((char*)this,sizeof(student)); ob.seekp(0,ios_base::beg); ob.read((char*)temp,sizeof(student)*4); disp(temp); ob.close(); } }; int main(int argc, char *argv[]) D.A.V. Centenary Public School Chander Nagar, Ghaziabad
{ student s1[4]={student(111,"aaa"),student(222,"bbb"),student(333,"ccc"), student(444,"ddd")}; s1[0].write(s1); s1[0].read(); s1[0].update(4); s1[0].read(); s1[0].delete1(3); s1[0].readWrite(); cin.get(); } Ex3: #ifndef EMP_H #define EMP_H #include<string> #include<fstream> using namespace std; class Emp { public: int empNum; string name; double sal; Emp(){} Emp(int a,string b,double c) { empNum=a; name=b; sal=c; } void disp() { cout<<empNum<<"\t"<<name<<"\t"<<sal<<endl; } void storeToFile() { ofstream ob1("f3.txt",ios::app); ob1.clear(); ob1<<empNum<<"\t"<<name<<"\t"<<sal<<endl; ob1.close(); } void readFromFile() { ifstream ob1("f3.txt"); while(ob1>>empNum>>name>>sal) { disp(); } ob1.close(); } void storeArrayToFile(Emp a[4]) D.A.V. Centenary Public School Chander Nagar, Ghaziabad
{ char na[10]; cin>>na; ofstream ob1(na,ios::app); for(int i=0;i<4;i++) ob1<<a[i].empNum<<"\t"<<a[i].name<<"\t"<<a[i].sal<<endl; ob1.close(); } void update(int n) { ifstream ob1("f66.txt"); int i=0; Emp e1[10]; while(ob1>>e1[i].empNum>>e1[i].name>>e1[i++].sal); ob1.close(); cin>>e1[n].empNum>>e1[n].name>>e1[n].sal; this->storeArrayToFile(e1); } }; #endif // EMP_H #include<iostream> #include "Emp.h" using namespace std; int main(int argc, char *argv[]) { Emp e1[100]; ifstream ib1("D://f1.txt"); int i=0; while(ib1>>e1[i].empNum>>e1[i].name>>e1[i].sal) { e1[i].disp(); i++; } ib1.close(); e1[1].empNum=777; ofstream ob1("D://f4.txt"); for(int j=0;j<i;j++) { e1[j].disp(); ob1<<e1[j].empNum<<"\t"<<e1[j].name<<"\t"<<e1[j].sal<<endl; } ob1.close(); ifstream ib2("D://f4.txt"); ofstream ob2("D://f6.txt"); char ch; while(!ib2.eof()) { D.A.V. Centenary Public School Chander Nagar, Ghaziabad
ch=ib2.get(); ob2.put(ch); } ib2.close(); ob2.close(); cin.get(); } Ex5: #include<iostream> #include<fstream> using namespace std; int main(int argc, char *argv[]) { ofstream ob1("D://f1.txt"); ob1<<"BVB COLlege of Engineering"<<endl; ob1.seekp(-4,ios_base::cur); ob1.put('G'); ob1.close(); }