fgetc fgetwc
從資料流讀取字元。
int fgetc(
FILE *stream
);
wint_t fgetwc(
FILE *stream
);
參數
- stream
指標FILE結構。
傳回值
fgetc傳回字元當做讀取int或傳回EOF ,表示錯誤或檔案結尾。fgetwc傳回時,為 wint_t,寬的字元相對於讀取的字元,或會傳回WEOF ,表示錯誤或檔案結尾。對於這兩個函式中,使用feof或ferror錯誤與檔案結尾條件之間加以區別。如果讀取的錯誤發生時,會設定資料流錯誤指標。如果stream是NULL, fgetc和fgetwc中所述,叫用無效的參數處理常式中, 參數驗證。如果執行,則允許繼續執行,這些函式會設定errno到EINVAL ,並傳回EOF。
備註
每個函式會讀取目前的位置,關聯的檔案的單一字元stream。函式再增加相關聯的檔案指標 (如果有定義) 為指向下一個字元。如果資料流是在檔案結尾,則會設定資料流的檔案結尾標記。
fgetc相當於getc,但是會實作僅為函式,而不是函式和巨集。
fgetwc是的寬字元版本fgetc。 上面寫著c為多位元組字元或萬用字元給是否根據stream在文字模式或 binary 模式開啟。
與版本_nolock尾碼完全相同,不同之處在於它們並不受干擾從其他執行緒。
如需有關如何處理寬字元和文字和二進位模式中的多位元組字元的詳細資訊,請參閱 Unicode 文字和二進位模式中的資料流 I/O。
泛用文字常式對應
TCHAR。H 常式 |
_UNICODE & 未定義的 _MBCS |
定義的 _MBCS |
定義 _unicode 之後 |
---|---|---|---|
_fgettc |
fgetc |
fgetc |
fgetwc |
需求
Function |
所需的標頭 |
---|---|
fgetc |
<stdio.h> |
fgetwc |
<stdio.h> 或者 <wchar.h> |
其他的相容性資訊,請參閱相容性在簡介中。
範例
// crt_fgetc.c
// This program uses getc to read the first
// 80 input characters (or until the end of input)
// and place them into a string named buffer.
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
FILE *stream;
char buffer[81];
int i, ch;
// Open file to read line from:
fopen_s( &stream, "crt_fgetc.txt", "r" );
if( stream == NULL )
exit( 0 );
// Read in first 80 characters and place them in "buffer":
ch = fgetc( stream );
for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ )
{
buffer[i] = (char)ch;
ch = fgetc( stream );
}
// Add null to end string
buffer[i] = '\0';
printf( "%s\n", buffer );
fclose( stream );
}
輸入: crt_fgetc.txt
Line one.
Line two.
Output
Line one.
Line two.