A
Affan Syed
Hi,
I am getting this weird problem. I know what i am doing is strange.. i am
using C++ vectors and fopen, but for some reason if i used ofstream in the
similar scenario it would give me errors.
So here is what i do. I create a new node and insert it in a vecotr as
follows:
nodeCreator = new cNode(location, skew, offset,Beaconify,nodeId,directory);
gNodeVector.push_back(*nodeCreator);
//now lets delete the memory we created for the node
delete nodeCreator;
Then I call an initNode function for the recently added node *in* the
vector:
gNodeVector[gNodeVector.size()-1].initNode();//initialize this new node
formed!!
Inside the initNode function I assign to a class member FILE* outFile
different filenames on the bases of the node id as follows:
void cNode::initNode(){
string outputFileName;
char idBuff[15];
sprintf(idBuff, "%d",myId);
string id(idBuff);
if (isBeacon) {//set the state according to the type of node
myState =e_phase1Beacon;
beaconId= myId;
outputFileName = outDirectory+"beacon_"+id+".out";
outFile = fopen(outputFileName.c_str(), "w+");
}
else{
myState =e_phase1Node;
outputFileName = outDirectory+"node_"+id+".out";
outFile = fopen(outputFileName.c_str(), "w+");
}
fileOpened = true; //this allows me to close the file only if was ever
opened
}
Now the problem that i am getting is that all the output that i do at the
individual nodes (extracted at various time from the vector) goes to the
same file.. and that is the last file created using thre above call. A
little debugging showed the fopen() is returning the *same* file pointer
value for each new file opened. So what is happening makes sense, but WHY is
it returning the same fptr even when i am giving different file names?
I have wasted over a day on this seemingly simple problem (I first was
trying to do with ofstream but that didnt work out either). Do you see any
reason why .. and can any body provide a soln... I would really aprreciate
this !
Best Regards
Affan
I am getting this weird problem. I know what i am doing is strange.. i am
using C++ vectors and fopen, but for some reason if i used ofstream in the
similar scenario it would give me errors.
So here is what i do. I create a new node and insert it in a vecotr as
follows:
nodeCreator = new cNode(location, skew, offset,Beaconify,nodeId,directory);
gNodeVector.push_back(*nodeCreator);
//now lets delete the memory we created for the node
delete nodeCreator;
Then I call an initNode function for the recently added node *in* the
vector:
gNodeVector[gNodeVector.size()-1].initNode();//initialize this new node
formed!!
Inside the initNode function I assign to a class member FILE* outFile
different filenames on the bases of the node id as follows:
void cNode::initNode(){
string outputFileName;
char idBuff[15];
sprintf(idBuff, "%d",myId);
string id(idBuff);
if (isBeacon) {//set the state according to the type of node
myState =e_phase1Beacon;
beaconId= myId;
outputFileName = outDirectory+"beacon_"+id+".out";
outFile = fopen(outputFileName.c_str(), "w+");
}
else{
myState =e_phase1Node;
outputFileName = outDirectory+"node_"+id+".out";
outFile = fopen(outputFileName.c_str(), "w+");
}
fileOpened = true; //this allows me to close the file only if was ever
opened
}
Now the problem that i am getting is that all the output that i do at the
individual nodes (extracted at various time from the vector) goes to the
same file.. and that is the last file created using thre above call. A
little debugging showed the fopen() is returning the *same* file pointer
value for each new file opened. So what is happening makes sense, but WHY is
it returning the same fptr even when i am giving different file names?
I have wasted over a day on this seemingly simple problem (I first was
trying to do with ofstream but that didnt work out either). Do you see any
reason why .. and can any body provide a soln... I would really aprreciate
this !
Best Regards
Affan