Tips and Pitfalls

Formatted I/O


Newlines are significant. Lines of output, to STDOUT for example, is separated by newlines, a carriage return and a line feed. The printf() procedure does not automatically issue a newline with every call. You must explicitly write the newlines using the \n escape as part of the format string. Otherwise, your output will be strung together, rather unintelligably. Actually, this can be useful, as you can use multiple printf() calls to build a single line of output. On input, a text file consists of lines delimited by newlines. The file may be read line by line using getline(). The newline terminating each line is returned as part of the string. Note that getline() and putline() are two of the procedures dealing with strings that do not have a string length argument. It is assumed that the string buffer is allocated with the size SZ_LINE.

The % Character

To output a percent character (%) using any of the formatted output procedures, use two adjacent percent characters, %% in the format string.

      call printf ("Ratio: %f%%\n") 
          call pargr (ratio) 
Results in:

      Ratio: 12.34% 
(assuming the value of ratio is 12.34).

Buffered Output

Standard formatted output is normally buffered. The result is that output to STDOUT may not appear on the user's terminal right away. The buffer is flushed when it is full, at the end of the task, or when it is explicitly flushed. The buffer may be flushed with flush(), whose argument is the file descriptor of the stream, STDOUT for example. In some cases, particularly in deing stages of development, it may be desirable to have output appear more quickly. Rather than using flush() repeatedly, you may set the fio parameter F_FLUSHNL to YES with a call to fset(). This advises fio to flush the buffer whenever it prints a newline character. Thus, output will appear on every line. Output to STDERR always flushes on newlines.

The % Character
Buffered Output

Generated with CERN WebMaker