开发者

Are the C formatted I/O functions (printf, sprintf, etc) more popular than IOStream, and if so, why? [closed]

开发者 https://www.devze.com 2023-01-13 06:46 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit t开发者_开发技巧he help center for guidance. Closed 12 years ago.

I've been looking through a lot of code made by others lately and happened to notice everyone uses "printf" style C functions a lot, but the C++ functions learned in school (cout, specifically) don't seem so popular.

Is this a valid observation, and is there a reason for this? Convention?

Thanks,

R


Personally, I use printf over the iostream stuff (like cout) because I think it's clearer.

When you do formatting with iostream, you have to << all sorts of weirdness like setiosflags and setf. I can never remember which namespace all this stuff lives in, let alone what it all does. Even when I do, I'm disappointed with how verbose and unintuitive the code looks.

The formatting options with printf may seem illegible at first, but they're concise, clearly documented in a single manual page, and common to a wide range of languages.

Another advanage is that printf is stateless: Unlike with cout, I don't need to remember which member functions have been called on printf, or which byzantine concoction of flags has been <<'ed into it. This is a big plus for readability.


I think taste is one possible reason. Personally I find this:

printf("%8d: %s\n", customer->id, customer->name);

more readable than this:

std::cout << customer->id << ": " << customer->name << std::endl;

There's also the issue with localization. printf makes it possible to change the formatting to suit other languages and UI cultures, which becomes a major chore with iostreams, unless you use something like the Boost Format library.


There are several criticisms of the standard stream system -- most notably that it usually is not as performant as C's system, and that they don't allow reordering of the items to be formatted, which can make localization more difficult.

Personally, I use streams for most everything, because they allow me to have a function write to a console, a file, or a string, without having to modify the function.


Where I work, we use printf-style formatting. This is because we used to make heavy use of the MFC CString class and its printf-style Format method. We've been phasing out MFC, but haven't changed our string-formatting approach.

As for which one is designed better, see Who architected / designed C++'s IOStreams, and would it still be considered well-designed by today's standards?


The printf and scanf family functions have two major problems: type safety and memory safety. It's fairly easy to make a mismatch between the specification string and the variable length argument list that follows. Also, buffer overruns through scanf are a classic security vulnerability. In short, don't use them.

The C++ streams offer type and memory safety, and also formatting extensibility. They're much more powerful and generally easier to use than printf and scanf.

Also, as suggested by ShaderOp, Boost's format library provides the same safety, but makes the old C programmers feel more comfortable.


I'm going to guess printf is more widely used because

  • it was in use for quite a few years before C++ compilers and streams showed up
  • C is used more than C++
  • lots of I/O was done for things like the Windows API, for which printf is a natural fit amongst Open/Read/Write/Close/etc
0

精彩评论

暂无评论...
验证码 换一张
取 消