1. 程式人生 > >fread函式讀取檔案提前結束

fread函式讀取檔案提前結束

最近寫了一個讀unicode文字的函式,是用fread函式讀,但有時候不能讀完,後來網上查了下,見下文:

最近寫一個程式,發現用fread讀“.dat”檔案時不能讀完整個檔案,後來用hex格式觀察讀檔案退出位置的數字,才發現只要是讀到0x1A時,fread就認為結束!後來看了MSDN後知道用text模式開啟檔案時,系統預設CTRL+Z為檔案結束符,而0x1A剛好就是CTRL+Z的ASCII碼。另外在另一個文章裡面說到如果遇到“/r/n”,也將被對映為“/n”。因此如果是讀普通資料而非文字,以以下格式開啟檔案。

datafile = fopen("whatever.dat", "rb"); 這樣就可以解決以上問題!

在MSDN中,對於"t"和"b"開啟模式的原文如下:

Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output. When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to the mbtowc

 function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function).

If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. If t or b is prefixed to the argument, the function fails and returns NULL

.

原文地址