Solved

Formatted string input and output with cout

Posted on 2004-09-21
6
529 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:davev
  • 3
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
jhshukla earned 50 total points
ID: 12113223
char *str = "David" is UNSAFE!! do not do this. use new() instead
char *str = new char[strlen("David")+1];
strcpy(str, "David");
str[strlen("David")] = 0;

don't forget delete[] str;

or better version
char str[] = "David";

I did not hardcode the value of strlen because this could be in a function and str would need to get a copy of the original string.

=====

if you are willing to use stl string then your job is easy
string str = "David";
cout << str.substr(0,4) << endl;

if you cannot or do not want to use stl then you will have to write a substring function. unfortunately it is not included in the cstring library. but the good part is that it's trivial and easy to write.
then create a string as described above and do
cout << substr(str,0,4) << endl;

post comments if you need more help

jaydutt
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12122267
You've found by now, no doubt that the manipulator setw(4) sets the minimum width only and doesn't truncate the string.

jaydutt is right about using substr, but wrong about:

>char *str = "David" is UNSAFE!! do not do this. use new() instead
>char *str = new char[strlen("David")+1];

You should write:

const char *str = "David";

It was a compromise for C portability that a non-const char pointer is permitted to read-only memory in C++. I'd actually prefer it, if the compiler issued a warning about the assignment...

 char *str = "David";

Maybe some compilers do.

The const char* could not be used for something which gets '\0' poked into it (e.g. strtok), and couldn't therefore be used for a substr function which pokes a '\0' into the char array. The compiler will prevent you from making that mistake if you are "const-correct".
0
 

Author Comment

by:davev
ID: 12123046
Good points from the two who responded so far and I will take what was said into consideration. I had thought of using a substr type of approach, but I'd still like to find ways for cout or any of the other ostream classes to support *all* the formatter a function like printf such that a conversion from printf (":%5.4s:", str) will still produce ": Davi:" when used with cout. Any other feedback is greatly appreciated. Thanks.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 9

Expert Comment

by:jhshukla
ID: 12129363
write a function yourself!
string foo(const string format_string, const string print_string) should parse the format_string and insert appropriate contents from print_string in to the result. then you could use it like cout << foo("%5.4", str);

or the long way would be to derive a class from ostream and add ~10-20 of member variables and overload some functions from the ostream.
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12233513
I think you can do %3.7 with strings using sprintf. I saw it somewhere in one of these rooms.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12235138
By all means use sprintf - see http://www.cplusplus.com/ref/cstdio/sprintf.html but include <cstdio> header rather than <stdio.h>.

Unlike printf/fprintf, there is no reason not to mix sprintf with C++ IOStreams other than philosophical. It is much better if you can embrace the design philosophy of C++ to get comfortable with it. sprintf leaves you with thoughts like "How big should I make that buffer?"/"If I have a great big buiffer on the stack, how should I return a string?" etc. None of these questions are rocket science to resolve, but there's something ugly about (say) seeing sprintf formatting a buffer provided by std::vector<char>.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.net build a IF/Then Walkthrough Guide 1 208
Best book to learn C++ 4 78
Issues with C++ Class 19 92
Why  my code (program) build with old compiler? 11 37
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now