The difference between text mode and binary mode with file streams

Source: Internet
Author: User
FIO14-C. Understand the difference between text mode and binary mode with file streams skip to end of metadata
  • Created by Justin pincar, last modified by Carol J. lallier on Oct 14,201 3
Go to Start of metadata

Input and output are mapped into logical data streams whose properties are more uniform than their varous inputs and outputs. two forms of mapping are supported, one for text streams and one for binary streams. they differ in the actual representation of data as well as in the functionality of some C functions.

Text streams Representation

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

The following code opens the filemyfileAs a text stream:

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

Environments may model line breaks differently. for example, on Windows, this code writes 2 bytes (a carriage return and then a newline) to the file, whereas on POSIX systems, this code writes only 1 byte (a newline ).

fseek()

For a text stream, the offsetfseek()Must be either 0 or a value returned by an earlier successful call toftell()Function (on a stream associated with the same file) with a modeSEEK_SET.

ungetc()

Theungetc()Function causes the file position indicator to beUnspecifiedUntil all pushed-back characters are read. As a result, care must be taken that file-position-related functions are not used while this is true.

Binary streams Representation

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

The following code opens the filemyfileAs a binary stream:

char *file_name; /* Initialize file_name */ FILE *file = fopen(file_name, "wb");/* Check for errors */fputs("\n", file);

Regardless of environment, this code writes exactly 1 byte (a newline ).

fseek()

According to the C standard, a binary stream may be terminated with an unspecified number of null characters and need not meaningfully supportfseek()Callwith a modeSEEK_END. Consequently, do not callfseek()On a binary stream with a modeSEEK_END.

ungetc()

Theungetc()Function causes the file-position indicator to be decremented by 1 for each successful call, with the value being indeterminate if it is 0 before any call. As a result,ungetc()Must never be called on a binary stream where the file position indicator is 0.

Risk Assessment

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

The difference between text mode and binary mode with file streams

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.