ofstream * vs. ofstream

S

Squid Seven

I'm trying to use a pointer to an ofstream object and having problems:



ofstream *sessionFile = NULL;

if( directory == "" )
sessionFile = new ofstream( fileName.c_str(), ios::eek:ut );
else
{
string fullFileName( directory );
fullFileName.append( fileName, 1, fileName.length() );
ofstream sessionFile( fullFileName.c_str(), ios::eek:ut );
}

*sessionFile << deckFileName.c_str() << "~\n\n"
<< primaryRangeStart << "~\n"
<< primaryRangeLength << "~\n\n"
<< numberCardsTotal << "~\n\n";

for( int counter = 1; counter <= numberCardsTotal; counter++ )
*sessionFile << weights[ counter ] << "~\n";



The file is being created, but there's nothing in it! I've confirmed
that the if-contained line is being executed. If I change the code as
follows, it works just fine:



ofstream sessionFile( fileName.c_str(), ios::eek:ut );

sessionFile << deckFileName.c_str() << "~\n\n"
<< primaryRangeStart << "~\n"
<< primaryRangeLength << "~\n\n"
<< numberCardsTotal << "~\n\n";

for( int counter = 1; counter <= numberCardsTotal; counter++ )
sessionFile << weights[ counter ] << "~\n";



What gives? How can I use the pointer to stream data into the ofstream
object?
 
H

Howard

Squid Seven said:
I'm trying to use a pointer to an ofstream object and having problems:



ofstream *sessionFile = NULL;

if( directory == "" )
sessionFile = new ofstream( fileName.c_str(), ios::eek:ut );
else
{
string fullFileName( directory );
fullFileName.append( fileName, 1, fileName.length() );
ofstream sessionFile( fullFileName.c_str(), ios::eek:ut );

The line above is declaring a new ofstream variable named sessionFile. It's
not the pointer variable at all. And, it goes out of scope immediately, at
the following }.

If you want to use that same ofstream* pointer, then use:

sessionFile = new ofstream( fullFileName.c_str(), ios::eek:ut );


(BTW, I haven't looked at the rest of your code, once I saw this mistake.)

-Howard
 
P

Peter Kragh

Squid said:
ofstream *sessionFile = NULL;

if( directory == "" )
sessionFile = new ofstream( fileName.c_str(), ios::eek:ut );
else
{
string fullFileName( directory );
fullFileName.append( fileName, 1, fileName.length() );
ofstream sessionFile( fullFileName.c_str(), ios::eek:ut );
}

*sessionFile << deckFileName.c_str() << "~\n\n"
<< primaryRangeStart << "~\n"
<< primaryRangeLength << "~\n\n"
<< numberCardsTotal << "~\n\n";

for( int counter = 1; counter <= numberCardsTotal; counter++ )
*sessionFile << weights[ counter ] << "~\n";

A couple of things to consider:

1) If directory contains data, you create a ofstream object
(sessionFile) that immediately goes out of scope (is destroyed) and
after that you call operator<< with a null pointer -> undefined behavior.

2) Do you ever delete the object? ofstream objects buffer output so in
order to be sure that the data is actually written to the file, the
objects needs to be destroyed or you need to call close().
The file is being created, but there's nothing in it! I've confirmed
that the if-contained line is being executed. If I change the code as
follows, it works just fine:



ofstream sessionFile( fileName.c_str(), ios::eek:ut );

sessionFile << deckFileName.c_str() << "~\n\n"
<< primaryRangeStart << "~\n"
<< primaryRangeLength << "~\n\n"
<< numberCardsTotal << "~\n\n";

for( int counter = 1; counter <= numberCardsTotal; counter++ )
sessionFile << weights[ counter ] << "~\n";



What gives? How can I use the pointer to stream data into the ofstream
object?

See above.

HTH.
 
S

Squid Seven

Howard said:
The line above is declaring a new ofstream variable named sessionFile. It's
not the pointer variable at all. And, it goes out of scope immediately, at
the following }.

You're right - I didn't notice this because I had not yet tested a case
in which the contents of the "else" executed - but it's not the cause of
my problem.
 
S

Squid Seven

Peter said:
Squid said:
ofstream *sessionFile = NULL;

if( directory == "" )
sessionFile = new ofstream( fileName.c_str(), ios::eek:ut );
else
{
string fullFileName( directory );
fullFileName.append( fileName, 1, fileName.length() );
ofstream sessionFile( fullFileName.c_str(), ios::eek:ut );
}

*sessionFile << deckFileName.c_str() << "~\n\n"
<< primaryRangeStart << "~\n"
<< primaryRangeLength << "~\n\n"
<< numberCardsTotal << "~\n\n";

for( int counter = 1; counter <= numberCardsTotal; counter++ )
*sessionFile << weights[ counter ] << "~\n";


A couple of things to consider:

1) If directory contains data, you create a ofstream object
(sessionFile) that immediately goes out of scope (is destroyed) and
after that you call operator<< with a null pointer -> undefined behavior.

2) Do you ever delete the object? ofstream objects buffer output so in
order to be sure that the data is actually written to the file, the
objects needs to be destroyed or you need to call close().

That was it - as soon as i inserted an instruction to delete the
ofstream object at the end of the function, everything worked fine.
Thanks much Peter.
The file is being created, but there's nothing in it! I've confirmed
that the if-contained line is being executed. If I change the code as
follows, it works just fine:



ofstream sessionFile( fileName.c_str(), ios::eek:ut );

sessionFile << deckFileName.c_str() << "~\n\n"
<< primaryRangeStart << "~\n"
<< primaryRangeLength << "~\n\n"
<< numberCardsTotal << "~\n\n";

for( int counter = 1; counter <= numberCardsTotal; counter++ )
sessionFile << weights[ counter ] << "~\n";



What gives? How can I use the pointer to stream data into the
ofstream object?


See above.

HTH.
 
I

Ivan Johansen

Squid said:
What gives? How can I use the pointer to stream data into the ofstream
object?

I can see that you solved the problem, but you will avoid a lot of
trouble by not using pointers if you don't need them:

ofstream sessionFile;

if( directory == "" )
sessionFile.open( fileName.c_str(), ios::eek:ut );
else
{
string fullFileName( directory );
fullFileName.append( fileName, 1, fileName.length() );
sessionFile.open( fullFileName.c_str(), ios::eek:ut );
}

sessionFile << deckFileName.c_str() << "~\n\n"
<< primaryRangeStart << "~\n"
<< primaryRangeLength << "~\n\n"
<< numberCardsTotal << "~\n\n";

for( int counter = 1; counter <= numberCardsTotal; counter++ )
sessionFile << weights[ counter ] << "~\n";


Ivan Johansen
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top