Opening a file. Whats the difference...

G

GeeRay

Hi all,
I was reading a code of a software which write some data in a log
file. This software was written by another programmer and I need to
update some parts. Every time the previous programmer needed to append
some data to an output file he did like this:

ofstream outfile;
ostringstream ostr;
ostr << "outfile.log";
char* log_name = const_cast<char*>(ostr.str().c_str());
outfile.open(log_name, ios::eek:ut | ios::app);

but I always do like this:

ofstream outfile;
outfile.open("outfile.log", ios::eek:ut | ios::app);

What are the differences?
The previous programmer is supposed to have more experience in C++ and I
don't understand why he write such a piece of code to open a file.

Thanks.
 
J

Juha Nieminen

GeeRay said:
ofstream outfile;
ostringstream ostr;
ostr << "outfile.log";
char* log_name = const_cast<char*>(ostr.str().c_str());
outfile.open(log_name, ios::eek:ut | ios::app);

My eyes hurt. That's a really horrendous way of opening a file.
 
B

Bo Persson

Juha said:
My eyes hurt. That's a really horrendous way of opening a file.

Even worse is that the temporary returned by str() goes away at the
end of the expression. Dangling pointer, anyone?

This is just plain wrong!


Bo Persson
 
J

James Kanze

GeeRay wrote:
The difference is that your way is shorter, more concise.

And more idiomatic.
Beats me, too. Perhaps he used to have some expression for
forming the name of the file, like
ostr << "outfile" << ++somenumber << ".log";
and then when the need to have the expression disappeared, the
code was kept mostly intact... I can't figure out the need
for the const_cast, though.

Or the intermediate char* variable.

Maybe he was paid by the line.
 
G

GeeRay

GeeRay said:
Hi all,
I was reading a code of a software which write some data in a log
file. This software was written by another programmer and I need to
update some parts. Every time the previous programmer needed to append
some data to an output file he did like this:

ofstream outfile;
ostringstream ostr;
ostr << "outfile.log";
char* log_name = const_cast<char*>(ostr.str().c_str());
outfile.open(log_name, ios::eek:ut | ios::app);

but I always do like this:

ofstream outfile;
outfile.open("outfile.log", ios::eek:ut | ios::app);

What are the differences?
The previous programmer is supposed to have more experience in C++ and I
don't understand why he write such a piece of code to open a file.

Thanks.


Thanks to everybody for your replies.

I see you share my point of view. I thought there was some sort of
security issue and he did like that to avoid shellcodes or something.
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top