The difference between text mode and binary mode with file streams

FIO14-C. Understand the difference between text mode and binary mode with file streams

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.

时间: 2024-09-30 09:32:17

The difference between text mode and binary mode with file streams的相关文章

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从库查看主从同步状态 1 mysql> show slave status\G; 2 *************************** 1. row *************************** 3 Slave_IO_State: 4 Master_Host: 101.200.*.* 5 Master_User: backup 6 Master_Port: 3306 7 Connect_Retry: 60 8 Master_

Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'系列一:

主库添加log-bin-index 参数后,从库报这个错误: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 next l

Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'系列二:reset slave

reset slave会清除从库的所有复制信息.一般应用场景:如切换为不同的Master, 主从重做等: 1. 命令在slave上执行,执行前一定要stop slave. 2. 执行reset slave后,会清除复制相关的所有信息,包括:master.info, relay-log.info, 及无条件删除所有的中继日志(relay logs). 注意是无条件的,也就是不管理你Slave SQL线程是否把所有的relay log重放完了. 3. 注意,stop slave后,先保存show s

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> show slave status \G Slave_IO_Running: No Slave_SQL_Running: Yes Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 1236 Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in bi

手动创建binary log files和手动编辑binary log index file会有什么影响

一.了解Binary Log结构 1.1.High-Level Binary Log Structure and Contents • Binlog包括binary log files和index file• 每个binary log文件的前4字节是Magic Number,紧接着是一组描述数据修改的Events • The magic number bytes are 0xfe 0x62 0x69 0x6e = 0xfe 'b''i''n' • 每个Event包括header bytes和da

JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解

现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不足 内存泄露 线程死锁 锁争用(Lock Contention) Java进程消耗CPU过高 ...... 这些问题在日常开发中可能被很多人忽视(比如有的人遇到上面的问题只是重启服务器或者调大内存,而不会深究问题根源),但能够理解并解决这些问题是Java程序员进阶的必备要求.本文将对一些常用的JVM性能调优监控工具进行介绍,希望能起抛砖引玉之用.本文参考了网上很多资料,难以一一列举,在此对这些资料的

Java问题定位工具

JDK本身提供了很多方便的JVM性能调优监控工具,除了集成式的VisualVM和jConsole外,还有jps.jstack.jmap.jhat.jstat.hprof等小巧的工具 现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不足 内存泄露 线程死锁 锁争用(Lock Contention) Java进程消耗CPU过高 ...... 这些问题在日常开发中可能被很多人忽视(比如有的人遇到上面的问题只是重启服务器或者调大内存,而不会深究问题根源),

[转]JVM性能调优监控工具

http://my.oschina.net/feichexia/blog/196575?p=1#comments JDK本身提供了很多方便的JVM性能调优监控工具,除了集成式的VisualVM和jConsole外,还有jps.jstack.jmap.jhat.jstat.hprof等小巧的工具,本博客希望能起抛砖引玉之用,让大家能开始对JVM性能调优的常用工具有所了解. 现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不足 内存泄露 线程死锁 锁争

性能调优工具

[性能调优工具jps.jstack.jmap.jhat.jstat.hprof使用详解]http://my.oschina.net/feichexia/blog/196575 现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不足 内存泄露 线程死锁 锁争用(Lock Contention) Java进程消耗CPU过高 这些问题在日常开发中可能被很多人忽视(比如有的人遇到上面的问题只是重启服务器或者调大内存,而不会深究问题根源),但能够理解并解决这些