本文介紹了以二進制和文本模式編寫的文件的區別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
寫入以文本模式打開但在二進制模式下沒有發生的文件時會發生什么轉換?特別是在 MS Visual C 中.
What translation occurs when writing to a file that was opened in text mode that does not occur in binary mode? Specifically in MS Visual C.
unsigned char buffer[256];
for (int i = 0; i < 256; i++) buffer[i]=i;
int size = 1;
int count = 256;
二進制模式:
FILE *fp_binary = fopen(filename, "wb");
fwrite(buffer, size, count, fp_binary);
與文本模式:
FILE *fp_text = fopen(filename, "wt");
fwrite(buffer, size, count, fp_text);
推薦答案
我相信大多數平臺在處理流時都會忽略t"選項或text-mode"選項.然而,在 Windows 上,情況并非如此.如果您查看以下 fopen() 函數的說明:MSDN,你會看到指定"t"選項會有如下效果:
I believe that most platforms will ignore the "t" option or the "text-mode" option when dealing with streams. On windows, however, this is not the case. If you take a look at the description of the fopen() function at: MSDN, you will see that specifying the "t" option will have the following effect:
- 換行符 (' ') 將在輸出時轉換為 ' " 序列
- 回車/換行序列將在輸入時轉換為換行.
- 如果文件以追加模式打開,將檢查文件末尾是否有 ctrl-z 字符(字符 26),并在可能的情況下刪除該字符.它還將將該字符的存在解釋為文件結尾.這是從 CPM 時代(關于父母的罪孽一直延續到他們的孩子直到第三代或第四代)的不幸遺留.與先前陳述的意見相反,不會附加 ctrl-z 字符.
- line feeds (' ') will be translated to ' " sequences on output
- carriage return/line feed sequences will be translated to line feeds on input.
- If the file is opened in append mode, the end of the file will be examined for a ctrl-z character (character 26) and that character removed, if possible. It will also interpret the presence of that character as being the end of file. This is an unfortunate holdover from the days of CPM (something about the sins of the parents being visited upon their children up to the 3rd or 4th generation). Contrary to previously stated opinion, the ctrl-z character will not be appended.
這篇關于以二進制和文本模式編寫的文件的區別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!