Writing to a Text File (Thread Safe)

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: fprintf()

Post by sje »

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.
abulmo
Posts: 151
Joined: Thu Nov 12, 2009 6:31 pm

Re: fprintf()

Post by abulmo »

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:

Code: Select all

printf&#40;" %5lu %3d %+1.2f ", nodes, depth, best_score / 100.0&#41;;
To:

Code: Select all

cout << width&#40;5&#41; << nodes << " " << width&#40;3&#41; << depth << " "
     << setiosflags&#40;ios&#58;&#58;fixed&#41; << precision&#40;2&#41; << best_score/100.0 << " " 
     << endl;
fprintf is also usually faster than c++ iostream output.
sje wrote:These routines have no type safety and have no way of checking arguments against the format string.
Any recent compiler is able to check that the argument types match the formatting string.

Code: Select all

$ gcc -std=c99 -pedantic -W -Wall test.c 
test.c&#58; In function ‘main’&#58;
test.c&#58;4&#58;2&#58; attention &#58; format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ &#91;-Wformat=&#93;
  printf&#40;"%d\n", "hello, world");
  ^
$ clang -std=c99 -pedantic -W -Wall test.c 
test.c&#58;4&#58;17&#58; warning&#58; format specifies type 'int' but the argument has type 'char *' &#91;-Wformat&#93;
        printf&#40;"%d\n", "hello, world");
                ~~     ^~~~~~~~~~~~~~
                %s
1 warning generated.
$ icc -std=c99 -pedantic -W -Wall test.c 
test.c&#40;4&#41;&#58; warning #181&#58; argument is incompatible with corresponding format string conversion
  	printf&#40;"%d\n", "hello, world");
Richard
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: fprintf()

Post by sje »

I repeat:

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.
syzygy
Posts: 5566
Joined: Tue Feb 28, 2012 11:56 pm

Re: fprintf()

Post by syzygy »

sje wrote:I repeat:
Why?
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: fprintf()

Post by sje »

syzygy wrote:
sje wrote:I repeat:
Why?
Why seven trumpets, when only one should suffice? (Revelation 11:15)