std::setw() Not Working?

Discussion in 'C++' started by Mike Copeland, Jul 7, 2013.

  1. 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;
    Mike Copeland, Jul 7, 2013
    #1
    1. Advertising

  2. In article <>,
    says...
    > > 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?

    >
    > 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<{{
    Mike Copeland, Jul 7, 2013
    #2
    1. Advertising

  3. Mike Copeland

    Öö Tiib Guest

    On Monday, 8 July 2013 00:20:27 UTC+3, Mike Copeland wrote:
    > 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.
    Öö Tiib, Jul 7, 2013
    #3
  4. Mike Copeland

    James Kanze Guest

    On Sunday, July 7, 2013 9:13:38 PM UTC+1, David Harmon wrote:
    > On Sun, 7 Jul 2013 11:10:48 -0700 in comp.lang.c++,
    > (Mike Copeland) wrote,
    > > 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?


    > 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.)

    --
    James
    James Kanze, Jul 8, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew David Hills

    input streams, setw(), and strings vs. char*?

    Matthew David Hills, Sep 5, 2003, in forum: C++
    Replies:
    1
    Views:
    576
    John Harrison
    Sep 5, 2003
  2. Marc Schellens

    setw, width

    Marc Schellens, Oct 6, 2003, in forum: C++
    Replies:
    1
    Views:
    600
    Attila Feher
    Oct 6, 2003
  3. Alex Vinokur

    width() and setw()

    Alex Vinokur, Nov 4, 2003, in forum: C++
    Replies:
    0
    Views:
    567
    Alex Vinokur
    Nov 4, 2003
  4. Martin Magnusson

    Persistent ios::setw()

    Martin Magnusson, Jul 13, 2004, in forum: C++
    Replies:
    2
    Views:
    623
    Fraser Ross
    Jul 13, 2004
  5. [rob desbois]

    iomanip setw() and std::string

    [rob desbois], Feb 7, 2008, in forum: C++
    Replies:
    1
    Views:
    601
    James Kanze
    Feb 8, 2008
Loading...

Share This Page