C++ Streams vs. C-style IO? -


I was coding some C + code for a small hobbies project when I saw that I had a C-style operation I'm using IO printf , fopen , etc.).

Is C ++ projects considered "bad behavior" to include C Functions? What are the benefits of using currents on Si-style IO access?

This is a hot topic. / P>

Some people like to use C ++ IO because they are type-safe (you can not deviate between the type of object and type specified in the format string), and the rest with the rest Mode of C ++ flow of coding

However, there are logic for C IO tasks (my personal favorite). Some of them are:

  • They easily integrate with localization, because smallest strings are not broken to complete localization, and localization value with some implementation, Take them into the string, ...
  • You can see the format of written text directly (this can be really hard with stream operators).
  • There is no inlineing as there, and only one instance of the printf function, the generated code is small (it can be important in the embedded environment).
  • Faster than the C ++ function in some implementations.

    Personally, I do not consider it bad practice to use the C stream in C ++ code. Even recommending using them on the C ++ stream. Will I consider the bad style to use both in the same project as others have mentioned, in a relatively larger project, you probably will not use them directly, but you can use one of the cover functions (or classes) Will use the set. , Which will be best for your coding criteria, and your needs (localization, type security, ...). You can use one or other IO interfaces to implement this high level interface, but you probably will only use one.


    Edit: Function function related to localization of printf family. Please note that those information is only valid for some implementation.

    Instead of % instead of % m $ in the context of the reference, you can use it in the formatted string values Can be re-ordered. The following program on the standard program Hello World! will write.

      #include & lt; Stdio.h & gt; Int main () {printf ("% 2 $ s% 1 $ s \ n", "World!", "Hello"); Return 0; }   

    Consider translating this C ++ code:

      if (nb_files_deleted == 1) stream & lt; & Lt; "A file"; Other streams & lt; & Lt; Nb_file_deleted & lt; & Lt; "Files"; Stream & lt; & Lt; The directory \ "was deleted from \" & lt; & Lt; Directory & lt; & Lt; "\" \ N ";   

    This can be really hard. With code (with a library to handle localization), with code The string is not mixed. We can pass this string to the localization team, and if the code is a special case in some language, then the code will not be updated (in some languages, if the calculation of the object is 0, you can use the plural There are other languages, there are three forms for singular, one is used Two objects and one plural form ...).

      printf (ngettext ("A file deleted from directory \"% 2 $ s \ "", "% 1 $ d directory \ "% 2 $ s \" ", n), n, has been removed from dir;    

Comments