本文介紹了以二進制和文本模式編寫的文件的區(qū)別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
寫入以文本模式打開但在二進制模式下沒有發(fā)生的文件時會發(fā)生什么轉(zhuǎn)換?特別是在 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);
推薦答案
我相信大多數(shù)平臺在處理流時都會忽略t"選項或text-mode"選項.然而,在 Windows 上,情況并非如此.如果您查看以下 fopen() 函數(shù)的說明: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:
- 換行符 (' ') 將在輸出時轉(zhuǎn)換為 ' " 序列
- 回車/換行序列將在輸入時轉(zhuǎn)換為換行.
- 如果文件以追加模式打開,將檢查文件末尾是否有 ctrl-z 字符(字符 26),并在可能的情況下刪除該字符.它還將將該字符的存在解釋為文件結(jié)尾.這是從 CPM 時代(關(guān)于父母的罪孽一直延續(xù)到他們的孩子直到第三代或第四代)的不幸遺留.與先前陳述的意見相反,不會附加 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.
這篇關(guān)于以二進制和文本模式編寫的文件的區(qū)別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!