473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ofstream * vs. ofstream

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
5 18489

"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
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


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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: red floyd | last post by:
Is there any way to retrieve the filename given to a std::ofstream (passed in constructor or in ofstream::open())? Or, should I derive from ofstream (should probably be a template to handle...
3
by: Chase Bradford | last post by:
Hey all I have a class Foo, and I'm trying to overload the << operator for both the ostream and ofstream for it. This way I should have two seperate formats for output, one for files and another...
5
by: cpp | last post by:
When I create an instance of ofstream, what is the name of the member variable that holds the filename? For example: ofstream ofs("Output.txt"); cout << ofs.WhatIsThePathVariable; If there...
2
by: Marina | last post by:
I get an "access violation" when I use someting like this: @@@@@@@@@@@@@@ string tempo; const char *output; vector <ofstream> outs(3); .... .... open_output=(const char *)tempo.c_str();...
2
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
5
by: wobudui | last post by:
Hi everyboday, I have some trouble in dealing with the file stream. My souce code Listed hear: int main() { char buffer={0}; ofstream ofile.open("mydata.in",ios::app); ofile.seekp(10);...
5
by: Gary Wessle | last post by:
Hi I have a map<string, doublem_temperatures which gets updated often. I need to save the data to files corresponding to each string each time the map is updated, I am expecting about 80 files...
5
by: Joe Hesse | last post by:
Hi, I have a C++ function that writes to an ofstream object. I would like to sometimes use it to write to cout. I realize that cout is of type ostream which is not ofstream. Since cout is "kind...
15
by: aaragon | last post by:
Hello, does anyone have a clue about this error? and how to solve it? It seems to be trivial to me, but not for the compiler. I'm using g++ 4.2 on an Ubuntu Linux system: // main() .......
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.