C ++流与C风格IO? [英] C++ Streams vs. C-style IO?

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

问题描述

当我注意到我使用C风格的操作来访问IO( printf )时,我正在为一个小的爱好项目编写一些C ++。



在C ++项目中涉及C函数是否被认为是坏习惯?

解决方案

这是一个热门话题。



有些人喜欢使用C ++ IO,因为它们是类型安全的(你不能在对象的类型和格式字符串中指定的类型之间存在差异),并且流程更多当然还有其他的C ++编码方式。



然而,还有C IO函数的参数(我个人最喜欢的)。其中一些是:




  • 它们更容易集成本地化,因为整个字符串localize不会在较小的字符串中分解,一些实现定位器可以重新排序插入值的顺序,在字符串中移动它们...

  • 您可以直接看到将要写入的文本的格式(这可以是因为没有内联,并且只有 printf 函数的一个实例,生成的代码是


  • $ b b b b b

    就个人而言,我不会认为在C ++代码中使用C流的做法不好。 一些组织甚至建议通过C ++流使用它们。我认为坏的风格是在同一个项目中使用。一致性是我认为的关键。






    正如其他人所说,在一个相对较大的项目中,他们直接,但你会使用一套包装函数(或类),这将最适合你的编码标准,你的需求(本地化,类型安全,...)。您可以使用一个或其他IO接口来实现此更高级别的界面,但您可能只使用一个。






    编辑:添加一些关于与本地化相关的 printf 格式化函数系列的优点的信息。请注意,这些信息仅适用于某些实施。



    您可以使用%m $ c $ c>%通过索引引用参数,而不是顺序引用它们。这可以用于重新排序格式化字符串中的值。以下程序将在标准输出中写入 Hello World!

      #include< stdio.h> 
    int main(){
    printf(%2 $ s%1 $ s\\\
    ,World!,Hello);
    return 0;
    }

    考虑翻译此C ++代码:

      if(nb_files_deleted == 1)
    stream<< 一个文件;
    else
    stream<< nb_file_deleted<< files;
    stream<<从目录\<<目录<< \\\\
    ;

    > printf (以及像 gettext 来处理本地化),代码不会与字符串混合,因此我们可以将字符串传递给本地化团队,并且不必更新代码特殊情况下在某些语言中(在某些语言中,如果对象的计数为0,则使用复数形式,在其他语言中,有三种形式一种用于单数,一种当有两个对象和复数形式时...)

      printf(ngettext(一个文件从目录\%2 $ s \,
    从目录中删除%1 $ d个文件\%2 $ s\,
    n),
    n,dir);


    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.).

    Is it considered "bad practice" to involve C functions in C++ projects? What are the advantages of using streams over C-style IO access?

    解决方案

    This is an heated topic.

    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.

    However, there is also arguments for C IO functions (my personal favorites). Some of them are:

    • 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.

    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.


    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.


    Edit: 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.

    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\n", "World!", "Hello");
        return 0;
    }
    

    Consider translating this C++ code:

    if (nb_files_deleted == 1)
        stream << "One file ";
    else
        stream << nb_file_deleted << " files ";
    stream << removed from directory \"" << directory << "\"\n";
    

    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 ++流与C风格IO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆