By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,098 Members | 2,007 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,098 IT Pros & Developers. It's quick & easy.

ofstream * vs. ofstream

P: n/a
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::out );
else
{
string fullFileName( directory );
fullFileName.append( fileName, 1, fileName.length() );
ofstream sessionFile( fullFileName.c_str(), ios::out );
}

*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::out );

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?
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Squid Seven" <te*********@squidseven.com> wrote in message
news:zL********************@adelphia.com...
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::out );
else
{
string fullFileName( directory );
fullFileName.append( fileName, 1, fileName.length() );
ofstream sessionFile( fullFileName.c_str(), ios::out );


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::out );
(BTW, I haven't looked at the rest of your code, once I saw this mistake.)

-Howard
Jul 23 '05 #2

P: n/a
Squid Seven wrote:
ofstream *sessionFile = NULL;

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

*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::out );

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.

--
Peter
Jul 23 '05 #3

P: n/a


Howard wrote:
"Squid Seven" <te*********@squidseven.com> wrote in message
news:zL********************@adelphia.com...
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::out );
else
{
string fullFileName( directory );
fullFileName.append( fileName, 1, fileName.length() );
ofstream sessionFile( fullFileName.c_str(), ios::out );

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.
If you want to use that same ofstream* pointer, then use:

sessionFile = new ofstream( fullFileName.c_str(), ios::out );
(BTW, I haven't looked at the rest of your code, once I saw this mistake.)

-Howard

Jul 23 '05 #4

P: n/a


Peter Kragh wrote:
Squid Seven wrote:
ofstream *sessionFile = NULL;

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

*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::out );

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.

Jul 23 '05 #5

P: n/a
Squid Seven wrote:
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::out );
else
{
string fullFileName( directory );
fullFileName.append( fileName, 1, fileName.length() );
sessionFile.open( fullFileName.c_str(), ios::out );
}

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
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.