<bdo id='awFa6'></bdo><ul id='awFa6'></ul>
  • <legend id='awFa6'><style id='awFa6'><dir id='awFa6'><q id='awFa6'></q></dir></style></legend>
  • <tfoot id='awFa6'></tfoot>

    <i id='awFa6'><tr id='awFa6'><dt id='awFa6'><q id='awFa6'><span id='awFa6'><b id='awFa6'><form id='awFa6'><ins id='awFa6'></ins><ul id='awFa6'></ul><sub id='awFa6'></sub></form><legend id='awFa6'></legend><bdo id='awFa6'><pre id='awFa6'><center id='awFa6'></center></pre></bdo></b><th id='awFa6'></th></span></q></dt></tr></i><div id='awFa6'><tfoot id='awFa6'></tfoot><dl id='awFa6'><fieldset id='awFa6'></fieldset></dl></div>

        <small id='awFa6'></small><noframes id='awFa6'>

      1. C++ Streams 与 C 风格的 IO?

        时间:2023-10-18
        1. <i id='ZPrXu'><tr id='ZPrXu'><dt id='ZPrXu'><q id='ZPrXu'><span id='ZPrXu'><b id='ZPrXu'><form id='ZPrXu'><ins id='ZPrXu'></ins><ul id='ZPrXu'></ul><sub id='ZPrXu'></sub></form><legend id='ZPrXu'></legend><bdo id='ZPrXu'><pre id='ZPrXu'><center id='ZPrXu'></center></pre></bdo></b><th id='ZPrXu'></th></span></q></dt></tr></i><div id='ZPrXu'><tfoot id='ZPrXu'></tfoot><dl id='ZPrXu'><fieldset id='ZPrXu'></fieldset></dl></div>
        2. <legend id='ZPrXu'><style id='ZPrXu'><dir id='ZPrXu'><q id='ZPrXu'></q></dir></style></legend>

              <tbody id='ZPrXu'></tbody>

            • <small id='ZPrXu'></small><noframes id='ZPrXu'>

              <tfoot id='ZPrXu'></tfoot>

                  <bdo id='ZPrXu'></bdo><ul id='ZPrXu'></ul>

                  本文介绍了C++ Streams 与 C 风格的 IO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当我注意到我正在使用 C 风格的操作来访问 IO(printffopen 等)时,我正在为一个小型业余项目编写一些 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 函数的实例,因此生成的代码更小(这在嵌入式环境中很重要).
                  • 在某些实现中比 C++ 函数更快.
                  • They integrate more easily with localisation, as the whole string to localise is not broken up in smaller strings, and with some implementation the localizer can reorder the order of the inserted value, move them in the string, ...
                  • You can directly see the format of the text that will be written (this can be really hard with stream operators).
                  • As there is no inlining, and only one instance of the printf function, the generated code is smaller (this can be important in embedded environment).
                  • Faster than C++ function in some implementation.

                  就我个人而言,我不会认为在 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 C++ 中被 EOF 停止后如何恢复输入流? 下一篇:C++ 和 C 文件 I/O

                  相关文章

                  • <bdo id='73bPT'></bdo><ul id='73bPT'></ul>
                  <legend id='73bPT'><style id='73bPT'><dir id='73bPT'><q id='73bPT'></q></dir></style></legend>

                  <small id='73bPT'></small><noframes id='73bPT'>

                • <tfoot id='73bPT'></tfoot>

                  1. <i id='73bPT'><tr id='73bPT'><dt id='73bPT'><q id='73bPT'><span id='73bPT'><b id='73bPT'><form id='73bPT'><ins id='73bPT'></ins><ul id='73bPT'></ul><sub id='73bPT'></sub></form><legend id='73bPT'></legend><bdo id='73bPT'><pre id='73bPT'><center id='73bPT'></center></pre></bdo></b><th id='73bPT'></th></span></q></dt></tr></i><div id='73bPT'><tfoot id='73bPT'></tfoot><dl id='73bPT'><fieldset id='73bPT'></fieldset></dl></div>