[Solved]Problem with skipws I/O manipulator

Hello everyone!

I'm a bit confused about how to use the skipws I/O manipulator.

Suppose I have the following code:
1
2
3
4
string [ ] = "   Message";
cout.setf ( ios::skipws )
cout << string;
cout.unsetf ( ios::skipws )

I would expect the output of this to be:
 
Message

But instead it is:
1
2
   
   Message


So what am I doing wrong? I've searched wih Google, but I can't find anything that points me in the right direction.

Thank you in advance.
Last edited on
http://www.cplusplus.com/reference/iostream/manipulators/skipws/

When the skipws format flag is set, as many whitespace characters as necessary are read and discarded from the stream until a non-whitespace character is found before every extraction operation.
That means skipws is for input.
It also doesn't work for input here. If I try to use the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>

using namespace std;

int main ( )
{

    char string [ 100 ];
    ifstream infile ( "test" );

    infile >> skipws;
    infile.getline ( string, sizeof ( string ) );
    cout << string << '\n';

    return ( 0 );

}


Which will fetch the line " Hello" from the file test, my output is still:
 
             Hello

and not
Hello

Does anybody know what is going wrong?

Thanks in advance!
I've figured it out: you need to use the "ws" I/O manipulator, and not the "skipws" I/O manipulator!
Topic archived. No new replies allowed.