当我注意到我正在使用 C 风格的操作来访问 IO(printf
、fopen
等)时,我正在为一个小型业余项目编写一些 C++ 代码..
I was coding some C++ for a small hobby project when I noticed that I'm using C-style operations to access IO (printf
, fopen
, etc.).
在 C++ 项目中使用 C 函数是否被认为是不好的做法"?与 C 风格的 IO 访问相比,使用流有哪些优势?
Is it considered "bad practice" to involve C functions in C++ projects? What are the advantages of using streams over C-style IO access?
这是一个热门话题.
有些人更喜欢使用 C++ IO,因为它们是类型安全的(对象的类型和格式字符串中指定的类型之间不能有分歧),并且与 C++ 的其余部分更自然地流动编码方式.
Some people prefer to use the C++ IO since they are type-safe (you can't have divergence between the type of the object and the type specified in the format string), and flow more naturally with the rest of the C++ way of coding.
然而,也有关于 C IO 函数的参数(我个人的最爱).其中一些是:
However, there is also arguments for C IO functions (my personal favorites). Some of them are:
printf
函数的实例,因此生成的代码更小(这在嵌入式环境中很重要).printf
function, the generated code is smaller (this can be important in embedded environment).就我个人而言,我不会认为在 C++ 代码中使用 C 流是不好的做法.一些组织甚至建议通过 C++ 使用它们溪流.我认为不好的风格是在同一个项目中同时使用两者.我认为一致性是这里的关键.
Personally, I wouldn't consider it bad practice to use C stream in C++ code. Some organisations even recommend to use them over C++ stream. What I would consider bad style is to use both in the same project. Consistency is the key here I think.
正如其他人所指出的,在一个相对较大的项目中,您可能不会直接使用它们,但您会使用一组最适合您的编码标准和您的需求(本地化、类型安全,...).您可以使用一个或另一个 IO 接口来实现这个更高级别的接口,但您可能只会使用一个.
As other have noted, in a relatively large project, you would probably not use them directly, but you would use a set of wrapper function (or classes), that would best fit your coding standard, and your needs (localisation, type safety, ...). You can use one or the other IO interface to implement this higher level interface, but you'll probably only use one.
添加一些关于printf
格式化函数族与本地化相关的优点的信息.请注意,这些信息仅对某些实现有效.
adding some information about the advantage of printf
formatting function family relating to the localisation. Please note that those information are only valid for some implementation.
您可以使用 %m$
而不是 %
来通过索引引用参数,而不是顺序引用它们.这可用于对格式化字符串中的值重新排序.以下程序将在标准输出上写入 Hello World!
.
You can use %m$
instead of %
to reference parameter by index instead of referencing them sequentially. This can be used to reorder values in the formatted string. The following program will write Hello World!
on the standard output.
#include <stdio.h>
int main() {
printf("%2$s %1$s
", "World!", "Hello");
return 0;
}
考虑翻译这个 C++ 代码:
Consider translating this C++ code:
if (nb_files_deleted == 1)
stream << "One file ";
else
stream << nb_file_deleted << " files ";
stream << removed from directory "" << directory << ""
";
这真的很难.使用 printf
(以及像 gettext
处理本地化),代码不与字符串混合.我们因此可以将字符串传递给本地化团队,并且如果某些语言中存在特殊情况,则不必更新代码(在某些语言中,如果对象计数为 0,则使用复数形式,在其他语言中,有三种形式,一种是单数形式,一种是有两个宾语,一种是复数形式,...)
This can be really hard. With printf
(and a library like gettext
to handle the localization), the code is not mixed with the string. We can thus pass the string to the localization team, and won't have to update the code if there are special case in some language (in some language, if count of object is 0, you use a plural form, in other language, there are three forms one for singular, one when there is two object and a plural form, ...).
printf (ngettext ("One file removed from directory "%2$s"",
"%1$d files removed from directory "%2$s"",
n),
n, dir);
这篇关于C++ Streams 与 C 风格的 IO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!