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?
C/C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs (just like water and oil flowing through a pipe). In input operations, data bytes flow from an input source (such as keyboard, file, network or another program) into the program.
In this tutorial, we will learn about Java input/output streams and their types. In Java, streams are the sequence of data that are read from the source and written to the destination. An input stream is used to read data from the source. And, an output stream is used to write data to the destination.
FIN specifies the minimum partial F value that a variable must have to enter the analysis. As additional variables are entered into the analysis, the partial F for variables already in the equation changes. FOUT specifies the smallest partial F that a variable can have and not be removed from the model.
ofstream fout; // declares an object of type ofstream. // ifstream and ofstream are defined in. <fstream> fin. open("infile.
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:
printf
function, the generated code is smaller (this can be important in embedded environment).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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With