Hi,
I am trying to create a vector of a class which contains ofstream object as one of its member. The problem I am facing is that when I add single element to the vector, it work fine. As soon as I add another element to the vector, it changes the address of the ofstream object of the previously added element. As the address changes, I can not write to the file pointed to by that ofstream object. The example program in which I am just printing the elements value after adding two elements to the vector is given below:
#include<iostream>
#include<vector>
#include <fstream>
using namespace std;
class routecaseBasedCDRFile
{
public:
routecaseBasedCDRFile(const routecaseBasedCDRFile& obj);
routecaseBasedCDRFile() ;
~routecaseBasedCDRFile() ;
routecaseBasedCDRFile& operator=(const routecaseBasedCDRFile& rhs);
string routecaseName;
string myRouteCaseBasedFilename;
bool myRouteCaseBasedFileOpen;
unsigned long myRouteCaseBasedCDRFileSequenceNumber;
long myCurrentNumOfRecordsWrittenInRouteCaseBasedFile;
ofstream myRouteCaseBasedOutCDRfile; // This is the output file
bool isRouteCaseBasedFileOpen() {}
void closeRouteCaseBasedFile() {}
void checkRouteCaseBasedFileTimeLimit() {}
int openNewRouteCaseBasedFile() {}
//int openNewRouteCaseBasedFile(CallDetailRecord &cdr);
};
routecaseBasedCDRFile::routecaseBasedCDRFile(const routecaseBasedCDRFile &obj)
{
cout<<"routecaseBasedCDRFile::routecaseBasedCDRFile(const routecaseBasedCDRFile &obj)"<<endl;
*this = obj;
}
routecaseBasedCDRFile::routecaseBasedCDRFile()
{
cout<<"routecaseBasedCDRFile::routecaseBasedCDRFile()"<<endl;
}
routecaseBasedCDRFile::~routecaseBasedCDRFile()
{
cout<<"routecaseBasedCDRFile::~routecaseBasedCDRFile()"<<endl;
}
routecaseBasedCDRFile& routecaseBasedCDRFile::operator=(const routecaseBasedCDRFile& rhs)
{
cout<<"routecaseBasedCDRFile::operator=(const routecaseBasedCDRFile& rhs)"<<endl;
routecaseName = rhs.routecaseName;
myRouteCaseBasedFilename = rhs.myRouteCaseBasedFilename;
myRouteCaseBasedFileOpen = rhs.myRouteCaseBasedFileOpen;
myrouteCaseBasedcdrSeqNumber = rhs.myrouteCaseBasedcdrSeqNumber;
myRouteCaseBasedCDRFileSequenceNumber = rhs.myRouteCaseBasedCDRFileSequenceNumber;
myCurrentNumOfRecordsWrittenInRouteCaseBasedFile = rhs.myCurrentNumOfRecordsWrittenInRouteCaseBasedFile;
}
int main()
{
vector<routecaseBasedCDRFile> routecaseFileVector;
routecaseBasedCDRFile routecaseCDRFile1,routecaseCDRFile2;
routecaseCDRFile1.routecaseName = "no_routecase";
routecaseCDRFile1.myrouteCaseBasedcdrSeqNumber = 0;
routecaseCDRFile1.myRouteCaseBasedFileOpen = false;
routecaseCDRFile1.myCurrentNumOfRecordsWrittenInRouteCaseBasedFile = 0;
routecaseCDRFile1.myRouteCaseBasedCDRFileSequenceNumber = 0;
routecaseFileVector.push_back(routecaseCDRFile1);
for(vector<routecaseBasedCDRFile>::iterator i=routecaseFileVector.begin();i != routecaseFileVector.end();i++)
{
if((*i).routecaseName == "no_routecase")
{
(*i).myRouteCaseBasedFilename = "/tmp/file1.txt";
(*i).myRouteCaseBasedOutCDRfile.unsetf(ios::unitbuf);
(*i).myRouteCaseBasedOutCDRfile.rdbuf()->pubsetbuf(0,2000000);
(*i).myRouteCaseBasedOutCDRfile.open((*i).myRouteCaseBasedFilename.c_str());
}
}
for( vector<routecaseBasedCDRFile>::iterator j=routecaseFileVector.begin();j != routecaseFileVector.end();j++)
{
cout <<"PRINT 1::(*j).myRouteCaseBasedOutCDRfile is "<<(*j).myRouteCaseBasedOutCDRfile <<endl;
cout <<"PRINT 1::(*j).routecaseName is "<<(*j).routecaseName <<endl;
cout <<"PRINT 1::(*j).myRouteCaseBasedFileOpen is "<<(*j).myRouteCaseBasedFileOpen <<endl;
cout <<"PRINT 1::(*j).myRouteCaseBasedFilename is "<<(*j).myRouteCaseBasedFilename <<endl;
}
routecaseCDRFile2.routecaseName = "no_routecase_1";
routecaseCDRFile2.myrouteCaseBasedcdrSeqNumber = 0;
routecaseCDRFile2.myRouteCaseBasedFileOpen = false;
routecaseCDRFile2.myCurrentNumOfRecordsWrittenInRouteCaseBasedFile = 0;
routecaseCDRFile2.myRouteCaseBasedCDRFileSequenceNumber = 0;
routecaseFileVector.push_back(routecaseCDRFile2);
for(vector<routecaseBasedCDRFile>::iterator i=routecaseFileVector.begin();i != routecaseFileVector.end();i++)
{
if((*i).routecaseName == "no_routecase_1")
{
(*i).myRouteCaseBasedFilename = "/tmp/file2.txt";
(*i).myRouteCaseBasedOutCDRfile.unsetf(ios::unitbuf);
(*i).myRouteCaseBasedOutCDRfile.rdbuf()->pubsetbuf(0,2000000);
(*i).myRouteCaseBasedOutCDRfile.open((*i).myRouteCaseBasedFilename.c_str());
}
}
for( vector<routecaseBasedCDRFile>::iterator j=routecaseFileVector.begin();j != routecaseFileVector.end();j++)
{
cout <<"PRINT 2::(*j).myRouteCaseBasedOutCDRfile is "<<(*j).myRouteCaseBasedOutCDRfile <<endl;
cout <<"PRINT 2::(*j).routecaseName is "<<(*j).routecaseName <<endl;
cout <<"PRINT 2::(*j).myRouteCaseBasedFileOpen is "<<(*j).myRouteCaseBasedFileOpen <<endl;
cout <<"PRINT 2::(*j).myRouteCaseBasedFilename is "<<(*j).myRouteCaseBasedFilename <<endl <<endl;
}
return 0;
}
The value of myRouteCaseBasedOutCDRfile of the first element of the vector comes out to be different before and after adding the second element. I am not able to understand the reason behind it. And because of changed value, I can not access the file pointed to by it.
Any help would be useful.