python能不能用c打开文件_C/C++/Python等 使用二进制模式打开文件与不使用二进制模式的区别...
C語言中文本文件與二進制文件的區別 一、文本文件與二進制文件的定義 大家都知道計算機的存儲在物理上是二進制的,所以文本文件與二進制文件的區別并不是物理上的,而是邏輯上的。這兩者只是在編碼層次上有差異。 簡單來說,文本文件是基于字符編碼的文件,常
概述: 今天嘗試使用c++中的ifstream來讀取一個zip文件,結果發現每次都是讀取了451個字節就結束了(測試用的zip文件4M多)。
--------------------------------------------------
author: cs_cjl
website:
http://blog.csdn.net/cs_cjl
--------------------------------------------------
測試代碼: #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進制編輯器查看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
總結
以上是生活随笔為你收集整理的python能不能用c打开文件_C/C++/Python等 使用二进制模式打开文件与不使用二进制模式的区别...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中数据类型大小_详细解析Py
- 下一篇: 修改mysql 外删除用户_mysql添