Formatted string input and output with cout
Posted on 2004-09-21
I'm having an issue trying to format a char * using cout, so that I get the same result as with the printf example below:
char *str = "David";
printf ("%.4s\n", str);
this will print "Davi", but I'd like to do the same thing with cout. cout.width(4) or setw(4) work well with numbers but not with strings. Is there some other function or manipulator that does this? This seems like it should be very easy to do, but online help and MSDN has provided no insight that I could find. An example using formatted input for scanf (like using istrstream) would be useful as well. Thanks.