C/C++/Python等 使用二进制模式打开文件与不使用二进制模式的区别
来源:互联网 发布:数据营销行业分析 编辑:程序博客网 时间:2024/04/30 03:05
概述:
今天尝试使用c++中的ifstream来读取一个zip文件,结果发现每次都是读取了451个字节就结束了(测试用的zip文件4M多)。--------------------------------------------------
author: cs_cjl
website: http://blog.csdn.net/cs_cjl
--------------------------------------------------
author: cs_cjl
website: http://blog.csdn.net/cs_cjl
--------------------------------------------------
测试代码:
#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, char *argv[])
{
ifstream fs(L"d:/tic.zip", std::ios::binary);
if (fs.is_open ()) {
cout << "file is open" << endl;
}
if (fs.good ()) {
cout << "filestream is good" << endl;
}
char buf[200];
size_t total_size(0);
while (true) {
fs.read(buf, 200);
total_size += fs.gcount ();
if (!fs) {
cout << "read " << fs.gcount () << endl;
cout << "fs.good () : " << fs.good () << endl;
cout << "fs.eof () : " << fs.eof () <<endl;
cout << "fs.fail () : " << fs.fail () << endl;
break;
}
}
cout << "read total size: " << total_size << endl;
return 0;
}
#include <fstream>
using namespace std;
int main (int argc, char *argv[])
{
ifstream fs(L"d:/tic.zip", std::ios::binary);
if (fs.is_open ()) {
cout << "file is open" << endl;
}
if (fs.good ()) {
cout << "filestream is good" << endl;
}
char buf[200];
size_t total_size(0);
while (true) {
fs.read(buf, 200);
total_size += fs.gcount ();
if (!fs) {
cout << "read " << fs.gcount () << endl;
cout << "fs.good () : " << fs.good () << endl;
cout << "fs.eof () : " << fs.eof () <<endl;
cout << "fs.fail () : " << fs.fail () << endl;
break;
}
}
cout << "read total size: " << total_size << endl;
return 0;
}
通过16进制编辑器查看zip文件,发现第252个字节为0x1a,通过查看
ascii表: http://en.wikipedia.org/wiki/Ascii
发现它对应Ctrl+Z,由于历史原因,在字符模式下
当遇到这个字符时,读取会结束
结论:
一直以为 二进制模式 和 字符模式 的区别只是对换行符\r \n的处理的不同
通过这次测试发现除了对换行符的处理不同外,字符模式还会对一些控制字符进行处理
参考:
wikipedia ASCII: http://en.wikipedia.org/wiki/Ascii
stackoverflow Line reading chokes on 0x1A http://stackoverflow.com/questions/405058/line-reading-chokes-on-0x1a/405169#405169
0 0
- C/C++/Python等 使用二进制模式打开文件与不使用二进制模式的区别
- C fread的文本模式与二进制模式的区别
- C语言文件打开和二进制打开方式的区别
- C二进制打开文件
- c语言中文本模式和二进制模式的区别
- 打开文本二进制与文本模式的区别
- C语言读取文件中文本模式和二进制模式的区别
- 二进制模式与Text模式的区别
- C/C++中二进制与文本方式打开文件的区别
- 文件使用二进制模式打开情况下的换行的方法
- 文件使用二进制模式打开情况下的换行的方法
- 文件打开的文本模式和二进制模式
- 文件打开的文本模式和二进制模式
- 文件打开的文本模式和二进制模式
- 文件打开的文本模式和二进制模式
- C语言文本方式和二进制方式打开文件区别
- C语言文本方式和二进制方式打开文件区别
- C语言文本方式和二进制方式打开文件区别
- 字符数组与字符指针浅析
- IntegertoRoman
- 响应式Web设计在跨终端广告创意中的应用
- linux动态链接库和静态库的生成及使用
- Codeforces 380 C. Sereja and Brackets
- C/C++/Python等 使用二进制模式打开文件与不使用二进制模式的区别
- Container With Most Water
- android 关机闹钟
- C++怎样获得CTabCtrl标签/CListCtrl列标头的文字?
- js获取关于客户端显示屏幕的相关数据
- nohup命令让Linux下程序永远在后台执行
- 正则表达式全部符号解释
- Add Two Numbers
- How to create an EMF model from a Java application ?