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 6 1612
Affan Syed wrote: 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
On Thu, 18 Nov 2004 11:53:13 -0800 in comp.lang.c++, "Affan Syed"
<as***@usc.edu> wrote, 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.
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):outDirectory(directory),myClock(skew,of fset),
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,offs et,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
"Affan Syed" <as***@usc.edu> wrote in message news:<cn**********@gist.usc.edu>...
[snip] Here are the constructor and destructore for cNode:
cNode(Coordinate location, float skew, timeTicks offset, bool Beaconify, short nodeId, string directory):outDirectory(directory),myClock(skew,of fset), 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::outFile 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.
Alternatively use can std::list<cNode> instead of std::vector<cNode>
and call cNode::initNode after the push back. Looking forward to some thing enlightening. Regards Affan
regards,
Stephan Brönnimann br****@osb-systems.com
Open source rating and billing engine for communication networks.
Thanks... I did just that.. i.e. provided a closeFile func called at the
end... although this solves the problem, but i still cant understand why i
get a destrctor to the last cNode (replicated) entered when i insert a new
node node into the vector???
Affan
"Stephan Br?nnimann" <br****@hotmail.com> wrote in message
news:d1**************************@posting.google.c om... "Affan Syed" <as***@usc.edu> wrote in message news:<cn**********@gist.usc.edu>... [snip] Here are the constructor and destructore for cNode:
cNode(Coordinate location, float skew, timeTicks offset, bool Beaconify, short nodeId, string directory):outDirectory(directory),myClock(skew,of fset), 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::outFile 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. Alternatively use can std::list<cNode> instead of std::vector<cNode> and call cNode::initNode after the push back.
Looking forward to some thing enlightening. Regards Affan
regards, Stephan Brönnimann br****@osb-systems.com Open source rating and billing engine for communication networks.
"Affan Syed" <as***@usc.edu> wrote in message news:<cn**********@gist.usc.edu>... Thanks... I did just that.. i.e. provided a closeFile func called at the end... although this solves the problem, but i still cant understand why i get a destrctor to the last cNode (replicated) entered when i insert a new node node into the vector???
That is probably a bad idea. Your closeFile really should be in the
destructor. However, you have to realise what happens with object
lifetimes.
In particular, you have to be aware that a compiler may create
temporary copies of your class. These copies will be destroyed when
the compiler is done with them.
Common cases where the compiler inserts these copies:
* when calling a function, arguments may be copied
* when growing a vector, elements may be copied
* when initializing objects with expression of another type.
Now, if you have a file class, which always closes the file in
the dtor, you will have a problem. When the compiler makes a
copy, the destructor will close the file even though there still
is a copy of the FILE*.
The fact that you couldn't use ofstream is telling. ofstream has
the same problem, and "solves" it by preventing ofstream copies.
Apparently, you did try to copy the file. That is precisely why
sterams are better. Instead of a runtime bug, you had a compile
time bug.
Luckily, there is an easy solution. Download boost ( www.boost.org)
and use a boost::shared_ptr<std::ofstream>. It will close the file
only when the _last_ shared_ptr to that file is destroyed.
HTH,
Michiel Salter This discussion thread is closed Replies have been disabled for this discussion. Similar topics
4 posts
views
Thread by PHPkemon |
last post: by
|
2 posts
views
Thread by USENETSERVER |
last post: by
|
303 posts
views
Thread by mike420 |
last post: by
|
6 posts
views
Thread by Sebastian Kemi |
last post: by
|
80 posts
views
Thread by Bibby |
last post: by
|
17 posts
views
Thread by Joe Laughlin |
last post: by
|
24 posts
views
Thread by rudranee |
last post: by
|
54 posts
views
Thread by Boris |
last post: by
|
10 posts
views
Thread by HCB |
last post: by
| | | | | | | | | | |