formatting a string
-
I'll bet you're familiar with this problem. printf("this might crash: %s", integervalue); In code with thousands of string formatting statements, at least some of them might be malformed. Anyone has a solid solution to prevent this type of crash? anyone seen good and secured string formatter code? greetings, Niko
-
I'll bet you're familiar with this problem. printf("this might crash: %s", integervalue); In code with thousands of string formatting statements, at least some of them might be malformed. Anyone has a solid solution to prevent this type of crash? anyone seen good and secured string formatter code? greetings, Niko
-
hmm. this will crash too. The question is not: how can I format strings. but how can I prevent myself from crashes due to bad formatting parameters.
-
I'll bet you're familiar with this problem. printf("this might crash: %s", integervalue); In code with thousands of string formatting statements, at least some of them might be malformed. Anyone has a solid solution to prevent this type of crash? anyone seen good and secured string formatter code? greetings, Niko
The standard libraries IOStream libraries. That's what exactly what they're designed for. int i = 1; std::string s = "foo" std::cout << i << s; or in the case of printing to a buffer use std::ostringstream in preference to sprintf
If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling
-
The standard libraries IOStream libraries. That's what exactly what they're designed for. int i = 1; std::string s = "foo" std::cout << i << s; or in the case of printing to a buffer use std::ostringstream in preference to sprintf
If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling
As much as I dislike iostreams, I have to agree with Andrew. If the programmers can't get their formats straight then iostreams is an excellent choice. Then again, if your programmers can't get their formats straight, you are going to have much more problems than just printing. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
I'll bet you're familiar with this problem. printf("this might crash: %s", integervalue); In code with thousands of string formatting statements, at least some of them might be malformed. Anyone has a solid solution to prevent this type of crash? anyone seen good and secured string formatter code? greetings, Niko
Try Boost format library. It provides type-safe formatting very similar to
printf
. Robert-Antonio "CRAY is the only computer, which runs an endless loop in just 4 hours" -
I'll bet you're familiar with this problem. printf("this might crash: %s", integervalue); In code with thousands of string formatting statements, at least some of them might be malformed. Anyone has a solid solution to prevent this type of crash? anyone seen good and secured string formatter code? greetings, Niko
this can't be correct if
intergervalue
is effectively an integer... %s is waiting for a char pointer which represent a NULL-terminated C-style string.... to prevent this type of crash as you say, it is first to well name your variables. iValue for an integer for example, pcArray for a char pointer... the second thing is to be attentive when coding or you usecout
instead, but that'll be more difficult to catch error...
TOXCCT >>> GEII power
-
I'll bet you're familiar with this problem. printf("this might crash: %s", integervalue); In code with thousands of string formatting statements, at least some of them might be malformed. Anyone has a solid solution to prevent this type of crash? anyone seen good and secured string formatter code? greetings, Niko
As a summary of what the others have written... using
printf
or its variants, how to prevent a crash if the wrong format specifier is used? The answer is simple: You can't. You will have to use IOStreams, or possibly as another person mentioned, boost. Using iostreams is probably the simplest alternate option. Either that, or make sure your format specifiers always match the data types! Sometimes I feel like I'm a USB printer in a parallel universe.