std::setw() Not Working?

M

Mike Copeland

The following code doesn't work as I understand it should.
Specifically, the "setw" takes effect for only the data which
immediately follows it, whereas I thought it would work on all data
which follows until another "setw" is encountered. What am I missing?
TIA

int IFns = 976, IPts = 130, ITms = 978;
FILE *fv = fopen("oldfile.sav", "wt");
ofstream saveFile;
saveFile.open("savefile.sav");
fprintf(fv, "%6d%6d%6d\n", IFns, IPts, ITms);
saveFile << setw(6) << IFns << IPts << ITms << endl;
 
M

Mike Copeland

What you are missing is: unlike most of the stream formatting
modifiers, setw takes effect for only the data which
immediately follows it.
Bummer. This flies in the face of some opinions I've read on this.
Guess Google's not to be trusted... 8<{{
 
Ö

Öö Tiib

Bummer. This flies in the face of some opinions I've read on this.
Guess Google's not to be trusted... 8<{{

Google spits out all possible answers to any questions so you should pick
the answer. From online references ... en.cppreference.com seems to
be correct almost always. Like with all wikis ... when you see that
something is wrong there then fix it.
 
J

James Kanze

On Sun, 7 Jul 2013 11:10:48 -0700 in comp.lang.c++, (e-mail address removed)
(Mike Copeland) wrote,
What you are missing is: unlike most of the stream formatting
modifiers, setw takes effect for only the data which
immediately follows it.

Yes and no. That's only sort of correct.

All of the manipulators position formatting information in
std::ios_base. They're not around to reset it after it has been
used. The *convention* is that any inserter or extractor which
uses ios_base::width() reset it to 0, all of the inserters and
extractors in the standard follow this convention, and all well
written user inserters and extractors would do well to do so
too.

Doing so correctly is often non-trivial, however, and
programmers have been known to be careless, and write less than
perfect code. Most user defined operator<< will forward
(directly or indirectly) to standard operator<< (e.g. for
double, or for std::string); these will reset the width to
double. Probably too soon; imagine a << for a user defined
Complex which does something like "dest << '(' << value.real()
<< ',' << value.image() << ')'. If the user defined operator<<
does create the sentinal objects, and output individual
characters to the streambuf, however, there's a distinct chance
that it forgets to reset the width. There's also a distinct
chance that it ignores the width completely. And of course,
a perverse programmer could set the width to any arbitrary
value. (That is, after all, how std::setw works to begin with.)

And of course, a programmer who doesn't respect the conventions
could also reset the precision, or any of the other formatting
flags. (But such perversity would require explicit action,
rather than just forgetting 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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top