SUBJECT: Object Oriented Programming with C++
Prepared by:
Kharecha Vishal R(130170107035)
Mistry Raj S(130170107045)
Modi Dishant S.(130170107046)
Vishwakarma Govt.
Engg.College
FILE MODES:
PARAMETER PURPOSE
ios::app Append mode(APPend)
ios::ate Go to end of file after opening a file(AT
End)
ios::binary Open file in binary mode i.e. not text
mode
ios::in Open file for read only(default for
ifstream)
ios::nocreate If file does’t exists,open fails.
ios::noreplace If file already exists,open fails.
ios::out Open file for write only(default for
ofstream)
ios::trunc If the file exists,delete its
contents(TRINCate)
FILE POINTERS
ď‚— Each file object has associated with it two pointers which are integer
values. i.e. get pointer(input pointer) and put pointer(output pointer).
ď‚— get pointer: used to read data from a specified location of file.
ď‚— put pointer:used to write data into a file at a specified location of a file.
ď‚— We can change the get pointer or put pointer by using functions to
manipulate the pointers.
MODES AND FUNCTION
NAME
POINTER VALUE AND
PURPOSE
Read only mode Get pointer at begginning of
file
Write only mode Put pointer at begginning of
file
Append Mode Put pointer at the end of file
seekg() Moves get pointer to
specified location
seekp() Moves put pointer to
specified location
tellg() Returns the current position
of get pointer
tellp() Returns the current position
of put pointer
get() and put() functions
ď‚— The get() and put() functions can be used with file stream classes.
ď‚— Get() and put() functions handle one character at a time.
ď‚— PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
Void main()
{
char line[]=“object oriented programming”;
fstream file;
file.open(“data”,ios::in|ios::out);
int len = strlen(line);
for(int i=0;i<len;i++)
{file.put(line[i]);}
file.seekg(0);
char c;
while(file.eof()!=1)
{
file.get(c);
cout<<c;
}
}
OUTPUT: object oriented programming
write() AND read() FUNCTIONS
ď‚— These functions are used to write and read blocks of
binary data from a file.
SYNTAX:
write((char*)&var,sizeof(var));
address of the data buffer lenghts in bytes
read((char*)&var,sizeof(var));
ď‚— PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
void main()
{
int n=2009;
int m;
char c;
fstream file;
file.open(“data”,ios::out);
file.write((char *) &n, sizeof(n));
file.close();
file.open(“data”,ios::in);
file.read((char *) &n ,sizeof(m));
file.close();
cout<<“value of m= ”<<m;
}
OUTPUT:
value of m=2009
READING AND WRITING CLASS
OBJECTS
ď‚— PROGRAM:
#include<iosream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
class person
{
private:
char name[30];
int age;
public:
void getdata()
{
cout<<“give name”;
cin>>name;
cout<<“give age”;
cin>>age;
}
void show()
{
cout<<“name =”<<name<<endl;
cout<<“age =”<<age<<endl;
}
};
void main()
{
fstream file:
person p,q;
p.getdata();
cout<<endl<<“objects data before writing in a file”<<endl;
p.show();
file.open(“data”,ios::in | ios::out);
file.write((char *) &p,sizeof(p));
file.seekg(0);
cout<<<<endl<<“object data read from file”<<endl;
file.read((char *) &q, sizeof(q));
file.close();
q.show();
}

Working with file(35,45,46)

  • 1.
    SUBJECT: Object OrientedProgramming with C++ Prepared by: Kharecha Vishal R(130170107035) Mistry Raj S(130170107045) Modi Dishant S.(130170107046) Vishwakarma Govt. Engg.College
  • 3.
    FILE MODES: PARAMETER PURPOSE ios::appAppend mode(APPend) ios::ate Go to end of file after opening a file(AT End) ios::binary Open file in binary mode i.e. not text mode ios::in Open file for read only(default for ifstream) ios::nocreate If file does’t exists,open fails. ios::noreplace If file already exists,open fails. ios::out Open file for write only(default for ofstream) ios::trunc If the file exists,delete its contents(TRINCate)
  • 4.
    FILE POINTERS ď‚— Eachfile object has associated with it two pointers which are integer values. i.e. get pointer(input pointer) and put pointer(output pointer). ď‚— get pointer: used to read data from a specified location of file. ď‚— put pointer:used to write data into a file at a specified location of a file. ď‚— We can change the get pointer or put pointer by using functions to manipulate the pointers.
  • 5.
    MODES AND FUNCTION NAME POINTERVALUE AND PURPOSE Read only mode Get pointer at begginning of file Write only mode Put pointer at begginning of file Append Mode Put pointer at the end of file seekg() Moves get pointer to specified location seekp() Moves put pointer to specified location tellg() Returns the current position of get pointer tellp() Returns the current position of put pointer
  • 6.
    get() and put()functions  The get() and put() functions can be used with file stream classes.  Get() and put() functions handle one character at a time.  PROGRAM: #include<iostream.h> #include<conio.h> #include<fstream.h> #include<string.h> Void main() { char line[]=“object oriented programming”; fstream file;
  • 7.
    file.open(“data”,ios::in|ios::out); int len =strlen(line); for(int i=0;i<len;i++) {file.put(line[i]);} file.seekg(0); char c; while(file.eof()!=1) { file.get(c); cout<<c; } } OUTPUT: object oriented programming
  • 8.
    write() AND read()FUNCTIONS ď‚— These functions are used to write and read blocks of binary data from a file. SYNTAX: write((char*)&var,sizeof(var)); address of the data buffer lenghts in bytes read((char*)&var,sizeof(var));
  • 9.
     PROGRAM: #include<iostream.h> #include<conio.h> #include<fstream.h> #include<string.h> void main() { intn=2009; int m; char c; fstream file; file.open(“data”,ios::out); file.write((char *) &n, sizeof(n)); file.close();
  • 10.
    file.open(“data”,ios::in); file.read((char *) &n,sizeof(m)); file.close(); cout<<“value of m= ”<<m; } OUTPUT: value of m=2009
  • 11.
    READING AND WRITINGCLASS OBJECTS ď‚— PROGRAM: #include<iosream.h> #include<conio.h> #include<fstream.h> #include<string.h> class person { private: char name[30]; int age;
  • 12.
    public: void getdata() { cout<<“give name”; cin>>name; cout<<“giveage”; cin>>age; } void show() { cout<<“name =”<<name<<endl; cout<<“age =”<<age<<endl; } };
  • 13.
    void main() { fstream file: personp,q; p.getdata(); cout<<endl<<“objects data before writing in a file”<<endl; p.show(); file.open(“data”,ios::in | ios::out); file.write((char *) &p,sizeof(p)); file.seekg(0); cout<<<<endl<<“object data read from file”<<endl; file.read((char *) &q, sizeof(q)); file.close(); q.show(); }