Skip to end of metadata

 

Go to start of metadata

 

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, 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 file myfile as 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 offset for fseek() must be either 0 or a value returned by an earlier successful call to the ftell() function (on a stream associated with the same file) with a mode of SEEK_SET.

ungetc()

The ungetc() function causes the file position indicator to be unspecified until 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 file myfile as 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 support fseek() calls with a mode of SEEK_END. Consequently, do not call fseek() on a binary stream with a mode of SEEK_END.

ungetc()

The ungetc() 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.

最新文章

  1. Log4j源代码学习
  2. MyBatis入门学习教程-调用存储过程
  3. 使用maven profile实现多环境可移植构建(转自CSDN)
  4. checkbox勾选判断
  5. UltraISO向U盘写入镜像特别慢
  6. 让 SVN (TortoiseSVN)提交时忽略bin和obj目录
  7. 淘宝API Nodejs的实现
  8. 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别
  9. PNG文件格式具体解释
  10. 导致flash屏幕重绘的几种方式及避免重绘的方法
  11. mono 判断系统的网络是否可用
  12. CSS3之重新定义鼠标右键
  13. Unity中溶解shader的总结
  14. PTA 旅游规划(25 分)
  15. a 标签实现分享功能
  16. Codeforces551 C. GukiZ hates Boxes
  17. 【Ueditor】富文本编辑使用
  18. CCPC-Wannafly Winter Camp Day4 Div1 - 置置置换 - [DP]
  19. staticmethod()静态方法和classmethod类方法都是装饰器
  20. kettle学习笔记(九)——子转换、集群与变量

热门文章

  1. Spring 3 MVC: Themes In Spring-Tutorial With Example---reference
  2. JMeter 使用
  3. Java 8 被动迭代式特性介绍(转自IBM)
  4. C++链表与键值对
  5. webservice 发布到外网的时候
  6. css 实现页面加载中等待效果
  7. java 字符串转int
  8. oracle创建实例SID
  9. 解决UITabeleViewCell的分割线不能铺满问题
  10. 一条sql语句循环插入N条不同记录(转)