This section looks at the different basic techniques that can be used for performing screen output.
The different techniques are discussed in separate sections and the Test Results subsection later in this section does an execution speed comparison of the techniques.
The printf() function can be used to write a string to the standard output stream (stdout). The string that is passed to printf() can contain complex formatting information that makes printf() a handy tool for doing conversions from binary variable values to strings. printf(), in fact, allows you to combine strings with automatically converted variables into a single output string. printf() was already part of C, but can still be used in C++. This is how you write an array input of N strings to screen using printf():
for(int i = 0; i < N ; i++) { printf("Test %s n",input[i]); }
The Test Results section evaluates the speed of a printf() function compared to the other screen writing techniques.
The puts() function writes a string to the standard output stream (stdout). The terminating zero character of the string is replaced by a newline character. This is how you write an array input of N strings to screen using puts():
strcpy(tmp, "Test "); for(int i = 0; i < N ; i++) { strcpy(&tmp[5], (char*)input[i]); puts(tmp); }
Note that, unlike with printf(), it is impossible to give extra arguments and formatting to puts(). This is why a strcpy() is used to add the prefix Test. Another way to solve the prefix problem would be to use two separate calls to puts():
for(int i = 0; i < N ; i++) { puts("Test "); puts((char*)input[i]); }
The output of each puts() invocation will, however, appear on a separate output line.
The putc() and putchar() functions both write single characters to an output stream. putchar() writes to stdout , but putc() can be told to write to an alternative output stream. putchar(c) is therefore identical to putc(c, stdout) . Listing 12.1 shows how you can write an array input of N strings to screen using putchar() .
Both putc() and putchar() are implemented as functions as well as macros. The macro definitions take precedent. For using putchar() always as a function you have to undefine the macro. To undefine the putchar() macro, place the following line of code in your source file before putchar() is used:
#undef putchar
When you want to use putchar() as a function only for a certain call, force the function invocation by using putchar() as follows:
(putchar)(c)
Using puts() and putc() is certainly more laborious than, for instance, using printf(). puts(), however is a very fast output technique. The Test Results section evaluates its speed compared to the other screen-writing techniques.
Apart from the standard C functions for screen output, C++ programmers also have access to the streaming classes provided by that language. Table 12.1 shows the different streams which are available in C++.
cin | Standard input stream (istream class), default connected to keyboard. |
cout | Standard output stream (ostream class), default connected to screen. |
cerr | Standard output stream for errors (ostream class), default connected to screen. |
clog | Buffered version of cerr (ostream class), default connected to screen. |
The difference between cerr and clog is that cerr messages are processed immediately and clog messages are buffered. Messages that should appear onscreen as soon as possible should, therefore, be sent to cerr. If a message is not supposed to slow down program execution too much, clog should be used. This section demonstrates the use of cout so it can be compared to the other screen output functions.
The cout class can only be used in C++. It is as powerful as the printf() function in that it can also be used to convert binary values into strings and combine these values with normal strings into a single output string. The syntax of cout is very different from that of printf() , however. This is how you write an array input of N strings to screen using cout :
for(int i = 0; i < N ; i++) { cout << "Test " << input[i] << ' n'; }
Using cout like this does not write a string to screen immediately, however; the text appears only when the output buffer is flushed. Flushing the output buffer can be done by adding << endl or << flush to the output stream. This is how you write an array input of N strings to screen using cout and flushing the buffer after every write action:
This section compares the speed of the different techniques for writing text strings to the screen. Table 12.2 presents the timing results generated by the program in the accompanying file 12Source01.cpp.
Technique | Time (ms) |
---|---|
cout + endl or flush | 500 |
cout + n | 380 |
printf() | 440 |
puts + copy | 400 |
puts twice | 540 |
putchar macro | 610 |
putchar function | 670 |
Note that file output can also be achieved with the function described for screen output. This can be done by redirecting program output with the > sign. In order to write the test result of this section file, type the following DOS command:
12Souce01 > outputfile.txt
Note also that stdio and iostream have independent buffering, meaning that
printf ("A string printed using printf n"); cout << "testing cout" << endl;
might output:
A string printed testing cout
using printf
Therefore, is it better to choose stdio or iostream. If you really need both, you should explicitly flush before switching.