1

I have a c++ program like this:

#include<iostream>
using namespace std;
class A
{
public:
void display();
};
void A::display() {
cout<<"This is from first program";
}
int main()
{
A a1;
a1.display();
return 0;
}

I want to save a1 into a file and call the display function by using this object from another c++ program. Is it possible? Is it possible to have main() function in both c++ programs? I am very new in C++. Please help me.

  • Please explain the purpose of your program and we might be able to evaluate a better solution for you. Are you familiar with C++ classes? What exactly would you like to write from a1? Is it the message from `display()`? If that is the case, what would the other program do? – Chringo May 12 '17 at 18:52
  • I have a very large program containing many classes and their functions. After the execution of the program for the first time, it gives an output. That output is an input of another program. The output of the second program is again used as an input to the first program. At that time I have to run the whole program from the first to last. But the first program takes minimum 7-8 hours for complete execution. – Dhruba Jyoti Borah May 15 '17 at 10:23
  • Therefore, when I run the first program for the second time it takes unnecessarily 7-8 hours. That's why I think a procedure to store the content of some objects into a file. If it is possible, then I should not create all the objects again and again. So, in this case I want to store all the content of the object A into a file. – Dhruba Jyoti Borah May 15 '17 at 10:24
  • Possible duplicate of [How to implement serialization in C++](http://stackoverflow.com/questions/1809670/how-to-implement-serialization-in-c) – MB-F May 16 '17 at 11:26

2 Answers2

1

Given your comments, I believe that you're looking for std::ofstream in your first program and std::ifstream in your second program. If program A is your first program with a very large code base and program B is your second program which will want to display the data from program A, then program A would use std::ofstream and program B would use std::ifstream.

If you follow the links, you'll find the description of what they do and at the bottom of the page, there's a code example. Don't start with program A that takes 7-8 hours to compute. You'll have to make sure that the output is correct before you can even use the data for input and any mistake on the way means that you'll have to restart. If you want to test and manually verify your data you can skip the second parameter as such, std::ofstream ostrm(filename) but then you need to rework how you operate the stream. Depending on your data, this will be the critical part of the operation.

// Code example from cppreference - ofstream
#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::string filename = "Test.b";
    {
        // This will create a file, Test.b, and open an output stream to it.
        std::ofstream ostrm(filename, std::ios::binary);
        // Depending on your data, this is where you'll modify the code
        double d = 3.14;
        ostrm.write(reinterpret_cast<char*>(&d), sizeof d); // binary output
        ostrm << 123 << "abc" << '\n';                      // text output
    }

    // Input stream which will read back the data written to Test.b
    std::ifstream istrm(filename, std::ios::binary);
    double d;
    istrm.read(reinterpret_cast<char*>(&d), sizeof d);
    int n;
    std::string s;
    istrm >> n >> s;
    std::cout << " read back: " << d << " " << n << " " << s << '\n';
}

With your data correctly outputted to a file you'll be able to read from it by creating the std::ifstream. Make sure to verify that the stream is open:

std::ifstream istrm(filename, std::ios::binary);
if(istrm.is_open())
{
    //Process data
}
Chringo
  • 131
  • 2
  • 12
0

This will be your File.h

#ifndef FILE_H
#define FILE_H

class A
{
public:
void display();
};
#endif

This will be your File.cpp

#include <iostream>
#include "File.h"

void A::display() {
std::cout<<"This is from first program";
}

This will be you main.cpp

#include <iostream>
#include "File.h"

int main()
{
A a1;
a1.display();
return 0;
}

You compile this with g++ File.cpp main.cpp

Manvir
  • 769
  • 1
  • 7
  • 15