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 ...
随机推荐
- [转] How to dispatch a Redux action with a timeout?
How to dispatch a Redux action with a timeout? Q I have an action that updates notification state of ...
- Java基础知识强化之集合框架笔记16:List集合的特有功能概述和测试
1. List集合的特有功能概述: (1)添加功能: void add(int index, Object element):在指定位置添加元素 (2)获取功能: Object get(int ind ...
- AndroidStudio工程文件导入Jar包和So第三方库
AndroidStudio 导入Jar包和第三方So库 在android开发中,需要导入许多第三方的jar包和so库来支持,包括像许多第三方的支持平台--友盟,环信.融云.极光推送.微博.腾讯等第三方 ...
- C# ashx与html的联合使用
本文将介绍ashx和html的联合使用方法,尽管目前流行mvc,但handler一般处理程序还是ASP.NET的基础知识,结合html页面,做出来的网页绝对比WebForm的简洁和效率高. 首先,概要 ...
- Shell - 特殊变量
$0 表示所执行程序的路径名. [huey@huey-K42JE ~]$ ll ~/bin total 4 -rwxrwxr-x 1 huey huey 21 Oct 24 14:39 hello [ ...
- SimpleDateFormat使用详解
http://blog.csdn.net/gubaohua/article/details/575488 public class SimpleDateFormat extends DateForma ...
- Alljoyn 概述(3)
开发工具 • scons:一个 Python写的自动化构建工具,是对 gnu make 改进的替代工具 • D-Feet:一个D-Bus调试工具 • C++ Code Generator Tool ( ...
- wpf 窗体中显示当前系统时间
先看一下效果: 这其实是我放置了两个TextBlock,上面显示当前的日期,下面显示时间. 接下来展示一下代码: 在XAML中: <StackPanel Width="205" ...
- js做全选,用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false
用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false,当所有checkbox都被选中时,全选按钮也被选中. 详解: 有两种 ...
- javascript基础学习(十)
javascript之数组 学习要点: 数组的介绍 定义数组 数组元素 数组的方法 一.数组的介绍 数组中的元素类型可以是数字型.字符串型.布尔型等,甚至也可以是一个数组. 二.定义数组 1.通过数组 ...