gpt4 book ai didi

c++ - 我应该切换到 C++ I/O 流吗?

转载 作者:IT老高 更新时间:2023-10-28 23:09:34 27 4
gpt4 key购买 nike

我从来没有过多地使用 C++ I/O 流,并且一直选择我所知道的。即 printf 函数。

我知道使用 I/O 流有一些好处,但我正在寻找一些技巧从 stackoverflow 社区帮助我(或说服我)切换。因为我还更喜欢 printf,我认为 printf 风格更容易阅读和打字。

即使我仍然继续使用 printf,我仍然想熟悉它。


编辑。有趣的是,谷歌 C++ 编码风格禁止使用除日志记录之外的流。见:http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

Streams

Use streams only for logging. Definition: Streams are a replacement for printf() and scanf().

Pros: With streams, you do not need to know the type of the object you are printing. You do not have problems with format strings not matching the argument list. (Though with gcc, you do not have that problem with printf either.) Streams have automatic constructors and destructors that open and close the relevant files.

Cons: Streams make it difficult to do functionality like pread(). Some formatting (particularly the common format string idiom %.*s) is difficult if not impossible to do efficiently using streams without using printf-like hacks. Streams do not support operator reordering (the %1s directive), which is helpful for internationalization.

Decision: Do not use streams, except where required by a logging interface. Use printf-like routines instead.

There are various pros and cons to using streams, but in this case, as in many other cases, consistency trumps the debate. Do not use streams in your code.

Extended Discussion

There has been debate on this issue, so this explains the reasoning in greater depth. Recall the Only One Way guiding principle: we want to make sure that whenever we do a certain type of I/O, the code looks the same in all those places. Because of this, we do not want to allow users to decide between using streams or using printf plus Read/Write/etc. Instead, we should settle on one or the other. We made an exception for logging because it is a pretty specialized application, and for historical reasons.

Proponents of streams have argued that streams are the obvious choice of the two, but the issue is not actually so clear. For every advantage of streams they point out, there is an equivalent disadvantage. The biggest advantage is that you do not need to know the type of the object to be printing. This is a fair point. But, there is a downside: you can easily use the wrong type, and the compiler will not warn you. It is easy to make this kind of mistake without knowing when using streams.

cout << this;  // Prints the address 
cout << *this; // Prints the contents

The compiler does not generate an error because << has been overloaded. We discourage overloading for just this reason.

Some say printf formatting is ugly and hard to read, but streams are often no better. Consider the following two fragments, both with the same typo. Which is easier to discover?

cerr << "Error connecting to '" << foo->bar()->hostname.first
<< ":" << foo->bar()->hostname.second << ": " << strerror(errno);
fprintf(stderr, "Error connecting to '%s:%u: %s",
foo->bar()->hostname.first, foo->bar()->hostname.second,
strerror(errno));

And so on and so forth for any issue you might bring up. (You could argue, "Things would be better with the right wrappers," but if it is true for one scheme, is it not also true for the other? Also, remember the goal is to make the language smaller, not add yet more machinery that someone has to learn.)

Either path would yield different advantages and disadvantages, and there is not a clearly superior solution. The simplicity doctrine mandates we settle on one of them though, and the majority decision was on printf + read/write.

最佳答案

使用 boost::format。两全其美。

关于c++ - 我应该切换到 C++ I/O 流吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4339523/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com