python能不能用c打开文件_C/C++/Python等 使用二进制模式打开文件与不使用二进制模式的区别...
C語言中文本文件與二進(jìn)制文件的區(qū)別 一、文本文件與二進(jìn)制文件的定義 大家都知道計(jì)算機(jī)的存儲(chǔ)在物理上是二進(jìn)制的,所以文本文件與二進(jìn)制文件的區(qū)別并不是物理上的,而是邏輯上的。這兩者只是在編碼層次上有差異。 簡(jiǎn)單來說,文本文件是基于字符編碼的文件,常
概述: 今天嘗試使用c++中的ifstream來讀取一個(gè)zip文件,結(jié)果發(fā)現(xiàn)每次都是讀取了451個(gè)字節(jié)就結(jié)束了(測(cè)試用的zip文件4M多)。
--------------------------------------------------
author: cs_cjl
website:
http://blog.csdn.net/cs_cjl
--------------------------------------------------
測(cè)試代碼: #include
#include
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 () <
cout << "fs.fail () : " << fs.fail () << endl;
break;
}
}
cout << "read total size: " << total_size << endl;
return 0;
}
通過16進(jìn)制編輯器查看zip文件,發(fā)現(xiàn)第252個(gè)字節(jié)為0x1a,通過查看 ascii表:
http://en.wikipedia.org/wiki/Ascii 發(fā)現(xiàn)它對(duì)應(yīng)Ctrl+Z,由于歷史原因,在字符模式下 當(dāng)遇到這個(gè)字符時(shí),讀取會(huì)結(jié)束
結(jié)論: 一直以為 二進(jìn)制模式 和 字符模式 的區(qū)別只是對(duì)換行符\r \n的處理的不同 通過這次測(cè)試發(fā)現(xiàn)除了對(duì)換行符的處理不同外,字符模式還會(huì)對(duì)一些控制字符進(jì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
總結(jié)
以上是生活随笔為你收集整理的python能不能用c打开文件_C/C++/Python等 使用二进制模式打开文件与不使用二进制模式的区别...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中数据类型大小_详细解析Py
- 下一篇: 模拟服务器和客户端交互的python脚本