C++ Read/write objects from/to file

C++ Read/write objects from/to file

We mentioned earlier that one of the shortcomings of the I/O system of C is that it cannot handle user defined data types such as class objects. since the class objects are the central elements of c++ programming, it is quite natural that the language supports features for writing to and reading from the disk files objects directly. The binary input and output functions read() and write() are designed to do exactly this job. These functions handle the entire structure of an object as a single unit, using the computers internal representation of data for instance, The function write() copies a class object from memory byte by byte with no conversion.one important point to remember is that only data members are written to the disk file and the member function are not

in the below program we see how class objects can be written to and read from the disk files The length of the object is obtained using the sizeof operator. This length represent the sum total of length of all data members of the object

READING AND WRITING CLASS OBJECTS
#include < iostream.h >
#include < fstream.h >
#include < iomanip.h >
class INVENTORY
{
char name[15];
int code;
float cost;
public:
void readdata();
void writedata();
};
void INVENTORY::readdata()
{
std::cout << "Enter NAme:";
std::cin >> name;
std::cout << "Enter Code:";
std::cin >> code;
std::cout << "Enter cost:";
std::cin >> cost;
}
void INVENTORY:: writedata() // formatted display on the screen
{
std::cout<setiosflag(std::iostream::left) <<="" setw(10)="" name << setiosflags(std::iostream::right)
<< setw(10) << code
<< setprecision(2)
<< setw(10) << cost;
}
void main()
{
INVENTORY item[3]; // declare Array of 3 objects
fstream file; // input and output file
file.open("STOCK.DAT",std::iostream::in | std::iostream::out);
std::cout << "Enter Details for Three items \n";
for(int i=0;i<3;i++)
{
item[i].readdata();
file.write((char *)&item[i],sizeof(item[i]));
}
file.seekg(0); //reset to start
std::cout<<"\n OUTPUT\n\n";
for(i=0;i<3;i++)
{
file.read((char *)&item[i],sizeof(item[i]));
item[i].writedata();
}
file.close();
}

output:
Enter Details for Three items
Enter NAme:C++
Enter Code:123
Enter cost:500
Enter NAme:C
Enter Code:124
Enter cost:600
Enter NAme:java
Enter Code:125
Enter cost:800


C++ 123 500
C 124 600
java 125 800

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise