bob wrote:Different strokes for different folks I suppose. I've spent too many years looking at operating system, compiler and such code.
I also have spent too many years starting in the 1970s looking at C code and it was not really very convenient to make the transition to C++. But it was worth it. To my eyes, C++ code can be much easier to read because of the abstraction and encapsulation capabilities of the language. However, it does take some skill on the part of the coder to employ these capabilities in an easy-to-read fashion.
For example, in C we have the printf(), fprintf(), and the sprintf() routines. But in C++, we have the ostream class and its "<<" concatenation operator, so building output is totally independent of the destination of the output. The same goes for input operations, swapping all the scanf() routines for the istream class.
sje wrote:I guess the question here is why anyone is using fprintf() and its pals in any code written this century. The last C program I wrote was a tablebase generator back in 1994; it's been C++ (and Lisp and Pascal) ever since. The C language formatted I/O routines are all antiques dating back forty years ago as copies of Fortran routines which were in use since the 1960s.
fprintf is better (i.e. is less verbose) than c++ iostream at formatting its output.
Compare:
$ gcc -std=c99 -pedantic -W -Wall test.c
test.c: In function ‘main’:
test.c:4:2: attention : format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
printf("%d\n", "hello, world");
^
$ clang -std=c99 -pedantic -W -Wall test.c
test.c:4:17: warning: format specifies type 'int' but the argument has type 'char *' [-Wformat]
printf("%d\n", "hello, world");
~~ ^~~~~~~~~~~~~~
%s
1 warning generated.
$ icc -std=c99 -pedantic -W -Wall test.c
test.c(4): warning #181: argument is incompatible with corresponding format string conversion
printf("%d\n", "hello, world");
1. The C++ ostream routines are destination independent. It takes three different output routines in C to do what is done with one in C++;
2. The C++ istream routines are origination independent. It takes three different input routines in C to do what is done with one in C++;
3. The C++ stream routines are all type safe. There is no need to keep track of a format string. There is no need to change a format string if the types of the objects are changed. The is no need to depend on compiler warnings, none of which can be issued anyway if the format string is not a literal.
4. The C++ stream routine operators can all be overloaded to handle complex types. An object can be changed from an array to a structure to a tree of arbitrary size and there's no need to re-code the final stream I/O calls.
5. C++ I/O does not depend on variable length argument lists, a sore spot in C with, at best, problematic run time checking.
6. C++ streams are far easier to teach and to learn. More importantly, they are far easier to read, and that's the biggest time saver.
It's time to give up on FORTRAN style I/O coding. FORTRAN had it's day, but that day was more than forty years ago.