Ravinder Singh

Friday, February 24, 1984
I am a software engineer by profession and programmer by passion. I love working on software projects which help make a positive impact on the society.

File operations using objects in C++

Category: Programming Tutorials Date: Tuesday, July 19, 2016

For the C++ training group , from our classroom discussions - 19th July, 2016

This is the code which you should practice in the main function to understand how to read and write data to a file using C++ fstream class.

#include <iostream>
#include <fstream>

#include "BusinessObjects/Branch.h"
#include "BusinessLogicLayer/BranchBL.h"

using namespace std;

int main()
    Branch *branch = new Branch();
    branch->SetName("Computer Science and Engineering");

    Branch branch1;// = new Branch();

    // Create object of the file handling class
    fstream fs;

    // Create the connection
    // Open the connection

    fs.open ("example2.dat", ios::out | ios::app | ios::binary);

    // Read or write the data
    fs.write( (char*)branch, sizeof(Branch) );

    fs.read((char*)&branch1, sizeof(Branch));

    cout << branch1.GetId();
    cout << "\n";
    cout << branch1.GetName();

    // Close the file connection.

    return 0;

Rate this article and help us improve

Please Login to rate
Overall ratings: 1 | Rating: 5 out of 5
Previous Article Next Article

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp