Advertisement
Binary I/O Functions | Description |
---|---|
read() | This binary function is used to perform file input operation i.e. to read the objects stored in a file. |
write | This binary function is used to perform file output operation i.e. to write the objects to a file, which is stored in the computer memory in a binary form. Only the data member of an object are written and not its member functions |
File I/O Stream Classes | Description |
---|---|
ifstream | This class is used to create an input stream and gives access to read() function, to perform the binary file input operation. |
ofstream | This class is used to create an output stream and gives access to write() function, to perform the binary file output operation. |
fstream | This class is used to used to create an input-output stream and gives access to both read() and write() function, to perform binary file input/output operations. |
file-stream-object("filename", mode);
Advertisement
File Mode | Description |
---|---|
ios::in | Searches for the file and opens it in read mode only and then we can use the binary input read() function to read the object stored in a file. |
ios::out | Searches for the file and opens it in write mode. Using this mode, we can use binary output write() function to write the object to a file on disk. |
read( (char *) & ob, sizeof(ob));
write( (char *) & ob, sizeof(ob));
//Writing a class object to a file using ofstream class and mode ios::out
#include<iostream>
#include<fstream>
using namespace std;
class A
{
private:
char name[40];
int age;
float height;
char gender;
public:
void putdata();
void getdata();
};
//Defining the function putdata() to enter the values of data members of an object.
void A :: putdata()
{
cout<<"Enter the name : ";
cin.getline(name,40);
cout<<"Enter the age : ";
cin>>age;
cout<<"Enter the height : ";
cin>>height;
cout<<"Enter the gender : ";
cin>>gender;
}
//Defining the function getdata() to read the values of data members of an object.
void A :: getdata()
{
cout<<"The name is : " << name << "\n";
cout<<"The age is : " << age << "\n";
cout<<"The height is : " << height << "\n";
cout<<"The gender is : " << gender << "\n";
}
int main()
{
//Creating an output stream
ofstream ofstream_ob;
//Calling the open function to write an object to a file
ofstream_ob.open("File9.txt", ios::out);
//Creating an object of A class
A ob1;
//Calling the putdata() function
ob1.putdata();
//Calling the write() function to write an object to a file.
ofstream_ob.write( (char *) & ob1, sizeof(ob1));
cout<<"Congrats! Your object is successfully written to the file \n";
//Closing the output stream
ofstream_ob.close();
//Creating an input stream
ifstream ifstream_ob;
//Calling the open function to read an object from a file
ifstream_ob.open("File11.txt", ios::in);
//Creating an empty object of A class
A ob2;
cout<<"\nReading the object from a file : \n";
//Calling the read() function to read an object from a file and transfer its content to an empty object
ifstream_ob.read( (char *) & ob2, sizeof(ob2));
//Calling the getdata() function
ob2.getdata();
//Closing the input stream
ifstream_ob.close();
return 0;
}
Enter the name : Arthur
Enter the age : 29
Enter the height : 1.78
Enter the gender : M
Congrats! Your object is successfully written to the file
Reading the object from a file :
The name is : Arthur
The age is : 29
The height is : 1.78
The gender is : M
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement