Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

title - text streams v binary streams

According to C99:

Input and output [...] are mapped into logical data streams, whose properties are more uniform than their various inputs and outputs. Two forms of mapping are supported, for text streams and for binary streams.

Text streams

Characters may have to be altered to conform to differing conventions for representing text in the host environment. As a consequence, data read/written to or from a text stream will not necessarily compare equal to the stream's byte content.

The following code opens the file myfile as a text stream:

FILE *file = fopen("myfile", "w");
/* Check for errors */
fputs("\n", file);

Some architectures might model line breaks differently. For example, on Windows, the above code will write two bytes (a carriage return and then a newline) to the file, whereas on *nix systems, it will only write one byte (a newline).

Binary streams

A binary stream is an ordered sequence of characters that can transparently record internal data. As a consequence, data read/written to or from a binary stream will necessarily compare equal to the stream's byte content.

The following code opens the file myfile as a binary stream:

FILE *file = fopen("myfile", "w");
/* Check for errors */
fputs("\n", file);

Regardless of architecture, this code will write exactly one character (a newline).

Risk Assessment

Failure to understand file stream mappings can result in unexpectedly formatted files.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

FIO07-A

1 (low)

2 (probable)

2 (medium)

P4

L3

References

[[ISO/IEC 9899-1999:TC2]] Section 7.14.1.1, "The signal fucntion"
[[Seacord 05a]]

  • No labels