The difference between text mode and binary mode with file streams
- Created by Justin Pincar, last modified by Carol J. Lallier on Oct 14, 2013
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.
The difference between text mode and binary mode with file streams的更多相关文章
- nodejs -- fs模块 ---> readFile 函数 1) fs.readFile(filename, "binary", function(error, file) 2) response.write(file, "binary");
一:代码: 1.1 入口文件: index.js var server = require('./server'); var router = require("./router" ...
- Canal 同步异常分析:Could not find first log file name in binary log index file
文章首发于[博客园-陈树义],点击跳转到原文Canal同步异常分析:Could not find first log file name in binary log index file. 公司搜索相 ...
- Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'系列一:
从库报这个错误:Got fatal error 1236 from master when reading data from binary log: 'Could not find first lo ...
- mysql从库Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'报错处理
年后回来查看mysql运行状况与备份情况,登录mysql从库查看主从同步状态 mysql> show slave status\G; *************************** . ...
- WARNING: Can not get binary dependencies for file...
环境: window7 64bit python 3.5 pyinstaller 3.2 用pyinstaller 将python文件打包成exe文件的过程中,出现了如下的错误 C:\Users\ca ...
- 'Could not find first log file name in binary log index file'的解决办法
数据库主从出错: Slave_IO_Running: No 一方面原因是因为网络通信的问题也有可能是日志读取错误的问题.以下是日志出错问题的解决方案: Last_IO_Error: Got fatal ...
- Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
setup slave from backup i got error Got fatal error 1236 from master when reading data from binary l ...
- text files and binary files
https://en.wikipedia.org/wiki/Text_file https://zh.wikipedia.org/wiki/文本文件
- 手动创建binary log files和手动编辑binary log index file会有什么影响
基本环境:官方社区版MySQL 5.7.19 一.了解Binary Log结构 1.1.High-Level Binary Log Structure and Contents • Binlog包括b ...
随机推荐
- 多路复用的server模型
多路复用I/O之server模型 主要是关于select()这个函数: 其原型是:int select(int n,fd_set *read_fds,fd_set *write_fds,fd_set ...
- (转载)JavaScript中面向对象那点事
鉴于自己在JavaScript这方面比较薄弱,所以就找了一本书恶补了一下(被称为犀利书的JavaScript权威指南).书的内容虽然多了点,但这也充分说明了js中的东西还是挺多的.虽然我们的定位不是前 ...
- U盘安装centos 6.4教程(总算是弄好了
参考:http://blog.chinaunix.net/uid-27666459-id-3342477.html http://www.linuxidc.com/Linux/2011-05/3569 ...
- Struts2和Struts1的不同
转载(没看懂) Action 类 ◆Struts1要求Action类继承一个抽象基类org.apache.struts.action.Action.Struts1的一个普遍问题是使用抽象类编程而不是接 ...
- IntelliJ Idea 常用快捷键列表(精简版)
查找快捷键: Ctrl+N 查找类 Ctrl+shift+N 查找文件 Ctrl+B 找变量来源 Ctrl+E 最近打开的文件 Ctrl+Alt+B 选中方法的实现 Ctrl+F7 选中方法(属性. ...
- win7 下配置 java 环境变量
首先,你应该已经安装了 java 的 JDK 了,笔者安装的是:jdk-7u7-windows-x64 接下来主要讲怎么配置 java 的环境变量,也是为了以后哪天自己忘记了做个备份 1.进入“计算机 ...
- asp.net手动填充TreeView生成树
最近在做项目发现需要用到树的地方,页面的前台任然是使用一个asp.net的控件TreeView来显示树的结构,当然也可以自己在前台写一个树来展示,这在后期跟局功能的不同很大可能会要用到异步的知识,废话 ...
- Android Animation学习 实现 IOS 滤镜退出动画
IOS的用户体验做的很好,其中一点很重要的地方就是动画效果. 最近在学习Android的Animation,简单实现了一个IOS相机滤镜退出的动画: 布局文件:activity_animation_d ...
- CentOS安装memcached及配置php的memcache扩展
遇到的问题: 这个问题主要是linux服务器安装memcached服务后,phpinfo信息没有memcache扩展,所以主要是给php安装memcache扩展,教程中是安装memcache扩展,我认 ...
- [o] SQLite数据库报错: Invalid column C
向SQLite数据库内新增列,之前出现过报错为提示no such column,通过删除并重建数据库文件解决,这次报错为无效的数据列: java.lang.IllegalArgumentExcepti ...