Why am i getting same file pointers every time

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
 
V

Victor Bazarov

Affan said:
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;

Now, this seems like an awful waste of CPU cycles. Why can't you just
write

gNodeVector.push_back(cNode(location,skew,
offset,Beaconify,nodeId,directory));

instead of that 'new/delete'?
Then I call an initNode function for the recently added node *in* the
vector:

gNodeVector[gNodeVector.size()-1].initNode();//initialize this new node
formed!!

Instead of doing size()-1 and indexing, you could just write

gNodeVector.back().initNode();
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);

This is extraneous. You could just add 'idBuff' to the string later.
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.

Have you checked that 'id' is different in every 'initNode' call?
A
little debugging showed the fopen() is returning the *same* file pointer
value for each new file opened.

So? The pointer value is probably just being reused, that's all.
So what is happening makes sense, but WHY is
it returning the same fptr even when i am giving different file names?

Do you keep all the files open or do you close them at some point?
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 !

Not enough information to give any specific answer, sorry. All I can
suggest at this point is to debug it thoroughly.

V
 
D

David Harmon

On Thu, 18 Nov 2004 11:53:13 -0800 in comp.lang.c++, "Affan Syed"
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;

1. You know, of course, that your cNode copy constructor and
assignment operator must be correct for this to work.

2. Unless there is something you haven't shown, the 'new' and
'delete' there are gratuitous and should be eliminated.

gNodeVector.push_back(
cNode(location, skew, offset,Beaconify,nodeId,directory));
Then I call an initNode function for the recently added node *in* the
vector:

gNodeVector[gNodeVector.size()-1].initNode();//initialize this new node
formed!!

I would prefer gNodeVector.back().initNode();
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.

This suggests to me that the files are getting closed prematurely.
It is reasonable for fopen to reuse memory if the files have been
closed.

Or perhaps you are somehow in fact losing the file pointers except
for the last one. Post more complete code - at least the
constructors, assignment operator, and destructor of class cNode.
 
A

Affan Syed

First of all i appreciate your reply...
1. You know, of course, that your cNode copy constructor and
assignment operator must be correct for this to work.
I am using defaults for both, but since my only pointer (that needs more
than a shallow copy) is the FILE* outFile, and I take care of it by
initializing it in a separate call .. hence the initNode() func. I control
the closing of file only if it was opened ( so i think this should cover all
scenarios?).

Here are the constructor and destructore for cNode:

cNode(Coordinate location, float skew, timeTicks offset, bool Beaconify,
short nodeId, string
directory):eek:utDirectory(directory),myClock(skew,offset),
myLocation(location), myId(nodeId),isBeacon(Beaconify){
fileOpened = false;
};
~cNode(){
if (fileOpened) {
fclose(outFile);
}

};
This suggests to me that the files are getting closed prematurely.
It is reasonable for fopen to reuse memory if the files have been
closed.
hmmm... but i am closing the file only when the destructor gets called. I
did further debuggin and i observe the following:
When i add the second node to the gnodeVector (I am now doing what you
suggested
i.e.gNodeVector.push_back(cNode(location,skew,offset,Beaconify,nodeId,directory));
), then the following things happen in order

1. A call to constructor of cNode is placed with a *new* id (say first one
way 1 and second one has id 2).
2. Then we have the call to the push_back() of vector and
3. before we get out of the above line I hit my break point in the
destructor of the cNode where it is closing a node with exactly the same
values (including the opened FILE*) and hence closes the file.

..So yes the file does get closed, but i still have *another* (I have no idea
how) inside the vector that i can search and use later. It seems that there
is some interaction with the vector push_back() that i cannot fathom and
solve.

Looking forward to some thing enlightening.
Regards
Affan
 
S

Stephan Br?nnimann

Affan Syed said:
Here are the constructor and destructore for cNode:

cNode(Coordinate location, float skew, timeTicks offset, bool Beaconify,
short nodeId, string
directory):eek:utDirectory(directory),myClock(skew,offset),
myLocation(location), myId(nodeId),isBeacon(Beaconify){
fileOpened = false;
};
~cNode(){
if (fileOpened) {
fclose(outFile);
}

};
[snip]
hmmm... but i am closing the file only when the destructor gets called. [snip]
.So yes the file does get closed, but i still have *another* (I have no idea
how) inside the vector that i can search and use later.

Yes, but that file is closed.
It seems that there
is some interaction with the vector push_back() that i cannot fathom and
solve.

std::vector::push_back() uses the copy constructor, i.e.
cNode::eek:utFile is always closed after the function returns.

Define a copy constructor and assignment operator for cNode
and provide debug messages from all constructors, destructors and
assignment operators. You'll learn a lot from the output!

You had some reason not to open the output file
in the constructor of cNode. Similarly you should provide
a function to close the file.
Looking forward to some thing enlightening.
Regards
Affan

regards,
Stephan Brönnimann
(e-mail address removed)
Open source rating and billing engine for communication networks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top