Comparing the two file stream libraries such as cstdio and fstream I conclude that they do exactly the same thing. I am studying C++. Is it better to continue with the "evolution" of the language and continue with fstream? The only thing that I think is different is that in cstdio there are the rename and remove functions to rename and delete files. Can this also be done with fstream?
stdio
(ocstdio
) is a legacy C library while itfstream
is a C++-only library. Differences? a few (I'll list a few):stdio
is inherited from C while itfstream
is native to C++iomanip
stdio
is inherited from C while itfstream
is native to C++In
stdio
you will find a collection of functions (not classes) designed to interact with the input/output, whilefstream
classes predominate (it is a C++ library for a reason).C++'s own input/output libraries allow the abstract and homogeneous use of its functionality
Being class-based, and thanks to the fact that these classes have inheritance, it is possible to implement input/output mechanisms independent of the final data source or destination:
This feature helps to reuse code. Okay yes, someone will tell me that
stdio
it can also be done with:The difference is, in this case, that making use of this feature
stdio
will prevent us from making use of practically half of the functionality offered by said library, since the functions that do not admit an input parameterFILE*
will not be able to be used in this context. The latter contrasts with the C++ own libraries that do not lose functionality.C++'s own libraries are extensible
The fact that
fstream
it is a C++ library implies that it has been designed with the characteristics of C++ in mind, which implies that, for example, its functionality is extensible through our own overloads:A similar design would be unthinkable with
stdio
or, at least, it would be more cumbersome to program and less natural, which would generate more complicated code to read.proprietary C++ libraries can make use of
iomanip
Another interesting feature of C++'s own input output is that it is possible to use the library
iomanip
to configure how the information is going to be dumped. This feature by itself doesn't add much, but when combined with the above features it makes for quite elegant solutions:conclusion
stdio
it exists basically because C++ was initially a kind of slightly vitaminized C. As C++ has been detaching itself from its predecessor, it has been acquiring its own libraries for its own interests.Currently
stdio
it serves more as a compatibility library to be able to bring C++ code into C++. If you are doing new code it is preferable to use C++'s own libraries.