setf(), precision(), and an ofstream reopened

J

jdm

I've got some code similar to the below in which a certain amount of
data is output during each iteration of a for loop.
I'm curious as to whether I need to call setf() and precision() again
every time I open the stream (it's opened for a different file each
time, so I can't just leave it open); or if they'll stay set after the
first time they're called, even if the stream is closed and then
reopened.

std::eek:fstream fout(test_filename.c_str(), ios::eek:ut);
if (!fout)
{
//Handle
}
fout<<"No data to output yet.";
fout.close();

for (current_run = first_run; current_run < no_of_runs; current_run++)
{
//Do some stuff with hillclimbs.

fout.open(output_filenames[current_run].c_str(), ios::eek:ut); //
output_filenames is a vector of std::string
fout.setf(ios_base::fixed, ios_base::floatfield);
fout.precision(8); //Do I REALLY need to call these on EVERY
loop iteration?

for (hc_cf_number_temp = 0; hc_cf_number_temp <= no_of_hillclimbs;
hc_cf_number_temp++)
{
fout<<costs_data[current_run][hc_cf_number_temp];
}

fout.close();
}

Thanks,

James McLaughlin.
 
J

James Kanze

I've got some code similar to the below in which a certain
amount of data is output during each iteration of a for loop.
I'm curious as to whether I need to call setf() and
precision() again every time I open the stream (it's opened
for a different file each time, so I can't just leave it
open); or if they'll stay set after the first time they're
called, even if the stream is closed and then reopened.

All of the formatting information will stay set until explicitly
reset, with the exception of width.
std::eek:fstream fout(test_filename.c_str(), ios::eek:ut);
if (!fout)
{
//Handle}
fout<<"No data to output yet.";
fout.close();
for (current_run = first_run; current_run < no_of_runs; current_run++)
{
//Do some stuff with hillclimbs.
fout.open(output_filenames[current_run].c_str(), ios::eek:ut); //
output_filenames is a vector of std::string
fout.setf(ios_base::fixed, ios_base::floatfield);
fout.precision(8); //Do I REALLY need to call these on EVERY
loop iteration?

No, but...
for (hc_cf_number_temp = 0; hc_cf_number_temp <= no_of_hillclimbs;
hc_cf_number_temp++)
{
fout<<costs_data[current_run][hc_cf_number_temp];

If you don't put some sort of separator in the output file, you
won't be able to reread the data.
 
J

jdm

All of the formatting information will stay set until explicitly
reset, with the exception of width.

Thanks! Do you have a reference for this?
If you don't put some sort of separator in the output file, you
won't be able to reread the data.

Actually, the overloaded << operator for costs_data (a struct) handles
that for me.

Cheers,

James McLaughlin.
 
J

James Kanze

Thanks! Do you have a reference for this?

The standard. It's been that way since the very beginning (but
the USL documentation of the original implementation was
considerably more readable than the standard).

Strictly speaking: the formatting information is handled by the
ios base class, and that class never changes the value of it
except as a result of an external command. For all of the
standard formatting output operators, however, the standard says
that width shall be reset to 0 once it has been used.
Logically, user defined operator<< should do likewise, but
there's nothing to enforce this.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top