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

Discussion in 'C++' started by Matthew David Hills, Sep 5, 2003.

  1. If setw() is being used to limit how many characters are pulled from
    a stream, should the # of characters taken from the stream depend on
    whether the stream is being sent to a std::string or a char*?

    string s1;
    char s2[256];
    cin >> setw(4) >> s1;
    cin >> setw(4) >> s2;

    (I've found there to be a difference between compilers -- on some,
    they results are identical (ie, 3 characters pulled), and on others, the
    string receives a 4th character)

    Thanks,
    Matt

    ----------------------------------------------------------------------
    #include<iostream>
    #include<string>
    #include<sstream>
    #include<iomanip>
    using namespace std;
    int main(void)
    {
    string s;
    char buffer[256];
    istringstream is1("0123456789");
    is1 >> setw(4) >> s;
    is1.seekg(0);
    is1 >> setw(4) >> buffer;

    cout << "s = " << s << endl;
    cout << "buf= " << buffer << endl;

    return 0;
    }
     
    Matthew David Hills, Sep 5, 2003
    #1
    1. Advertising

  2. "Matthew David Hills" <> wrote in message
    news:bj8mqj$4pl$...
    >
    > If setw() is being used to limit how many characters are pulled from
    > a stream, should the # of characters taken from the stream depend on
    > whether the stream is being sent to a std::string or a char*?
    >
    > string s1;
    > char s2[256];
    > cin >> setw(4) >> s1;
    > cin >> setw(4) >> s2;
    >
    > (I've found there to be a difference between compilers -- on some,
    > they results are identical (ie, 3 characters pulled), and on others, the
    > string receives a 4th character)
    >
    > Thanks,
    > Matt
    >


    Its a good point, and the answer is yes the number should vary. The reason
    is that setw is being used for subtly different reasons in the two cases.
    For std::string setw is that maximum numbers of characters to be extracted,
    for char* setw is intended as the size of the array that the char* is
    pointing at. Since C style strings require a null terminator only setw - 1
    characters can be extracted.

    john
     
    John Harrison, Sep 5, 2003
    #2
    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. Alex Vinokur

    width() and setw()

    Alex Vinokur, Nov 4, 2003, in forum: C++
    Replies:
    0
    Views:
    581
    Alex Vinokur
    Nov 4, 2003
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,096
    Ian Collins
    May 9, 2006
  3. alternativa
    Replies:
    4
    Views:
    604
    Alan Woodland
    May 17, 2006
  4. Jess
    Replies:
    3
    Views:
    404
  5. Christian Johannes Charbula

    ostream operator and setw

    Christian Johannes Charbula, Sep 14, 2007, in forum: C++
    Replies:
    12
    Views:
    688
    James Kanze
    Sep 15, 2007
Loading...

Share This Page