fopen的按文本读写和按二进制读写
2018-01-30 21:31
218 查看
二进制读写和文本读写的区别:
1. 二进制方式很简单,读文件时,会原封不动的读出文件的全部內容,写的時候,也是把內存缓冲区的內容原封不动的写到文件中。而‘\n’ 在ascii码中是’0x0A’。
2. 而文本方式就不一样了,在写文件时,会将换行符号‘\n’会转化为系统相应的ascii码,所以,在linux下,会把换行符转化为linux 的换行 ‘0x0A’ ;在windown下,会把换行符转化为‘0x0D 0x0A’。
加入,有下面代码
在linux下运行,分别按文本生成t.txt,按二进制生成b.txt,用hexdump -C命令查看,如下:
在vs2013下运行,产生相应文件
t.txt如下:
b.txt如下:
而在windown下,二进制是转化为’0x0A’ ,文本是转化为’0x0D 0x0A’。
1. 二进制方式很简单,读文件时,会原封不动的读出文件的全部內容,写的時候,也是把內存缓冲区的內容原封不动的写到文件中。而‘\n’ 在ascii码中是’0x0A’。
2. 而文本方式就不一样了,在写文件时,会将换行符号‘\n’会转化为系统相应的ascii码,所以,在linux下,会把换行符转化为linux 的换行 ‘0x0A’ ;在windown下,会把换行符转化为‘0x0D 0x0A’。
加入,有下面代码
nt main( ) { char he[20] = "hello world\n"; FILE *outfile = fopen("t.txt", "wt"); fwrite(he, sizeof(char), 20, outfile); fclose(outfile); outfile = fopen("b.txt", "wb"); fwrite(he, sizeof(char), 20, outfile); fclose(outfile); return 0; }
在linux下运行,分别按文本生成t.txt,按二进制生成b.txt,用hexdump -C命令查看,如下:
tl@tl-vm:~/program/c/test_in_clion/cmake-build-debug$ hexdump b.txt -C 00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 0a 00 00 00 00 |hello world.....| 00000010 00 00 00 00 |....| 00000014 tl@tl-vm:~/program/c/test_in_clion/cmake-build-debug$ hexdump t.txt -C 00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 0a 00 00 00 00 |hello world.....| 00000010 00 00 00 00 |....| 00000014
在vs2013下运行,产生相应文件
t.txt如下:
b.txt如下:
总结:
二进制标志,文本标志,在linux下是没有影响的,因为两种情况下,‘\n’都是‘0x0A’。而在windown下,二进制是转化为’0x0A’ ,文本是转化为’0x0D 0x0A’。
相关文章推荐
- fopen 中 按文本读写与按二进制读写 实例
- 二十七 fopen 中 按文本读写与按二进制读写 实例
- 文件读写:fwrite与fprintf的区别(即二进制方式和文本方式的区别)
- AS读写文件(文本、二进制)
- PHP读写大“二进制”文件,不必申请很大内存(fopen、fread、fwrite、fclose)
- 读写大“二进制”文件,不必申请很大内存(fopen,fread,fwrite,fclose)
- 关于文件的文本读写和二进制读写
- open语句对文本和二进制文件的读写
- IO流-文本IO\读写二进制数据
- fopen后只能读到部分文件问题解决,文本方式vs二进制方式
- 【C/C++学院】0826-文件重定向/键盘输入流/屏幕输出流/字符串输入输出/文件读写简单操作/字符文件读写二进制与文本差别/get与getline挖掘数据/二进制与文本差别/随机位置/多线程初级
- 文件读写:fwrite与fprintf的区别(即二进制方式和文本方式的区别)
- Java -- JDBC mysql读写大数据,文本 和 二进制文件
- 文件读写(文本形式/二进制形式)
- Java -- JDBC mysql读写大数据,文本 和 二进制文件
- 文件读写:fwrite与fprintf的区别(即二进制方式和文本方式的区别)
- 重新想象 Windows 8 Store Apps (23) - 文件系统: 文本的读写, 二进制的读写, 流的读写, 最近访问列表和未来访问列表
- 【C/C++学院】0826-文件重定向/键盘输入流/屏幕输出流/字符串输入输出/文件读写简单操作/字符文件读写二进制与文本差别/get与getline挖掘数据/二进制与文本差别/随机位置/多线程初级
- [置顶] fopen等:文本方式和二进制方式打开文件的区别
- 文件读写: 二进制方式和文本方式的区别