取得與資料流相關聯的檔案描述項。
語法
int _fileno(
FILE *stream
);
參數
stream
FILE 結構的指標。
傳回值
_fileno 會傳回檔案描述元。 不會傳回錯誤。 如果未 stream 指定開啟的檔案,則結果為未定義。 如果 stream 為 NULL,_fileno則會叫用無效的參數處理程式,如參數驗證中所述。 如果允許繼續執行,此函式會傳回 -1,並將 errno 設為 EINVAL。
如需這些錯誤碼和其他錯誤碼的詳細資訊,請參閱errno、 _doserrno_sys_errlist和 _sys_nerr。
注意
如果 stdout 或 stderr 與輸出資料流無關 (例如,在沒有主控台視窗的 Windows 應用程式中),傳回的檔案描述元是 -2。 在舊版本中,傳回的檔案描述元是 -1。 此變更可讓應用程式區分這個條件與錯誤。
備註
_fileno 常式會傳回目前與 stream 相關聯的檔案描述元。 此常式可當作函式和巨集來實作。 如需選擇任一實作的相關信息,請參閱 在函式和巨集之間選擇的建議。
根據預設,此函式的全域狀態會限定於應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。
需求
| 函式 | 必要的標頭 |
|---|---|
_fileno |
<stdio.h> |
如需相容性詳細資訊,請參閱相容性。
範例
// crt_fileno.c
// This program uses _fileno to obtain
// the file descriptor for some standard C streams.
//
#include <stdio.h>
int main( void )
{
printf( "The file descriptor for stdin is %d\n", _fileno( stdin ) );
printf( "The file descriptor for stdout is %d\n", _fileno( stdout ) );
printf( "The file descriptor for stderr is %d\n", _fileno( stderr ) );
}
The file descriptor for stdin is 0
The file descriptor for stdout is 1
The file descriptor for stderr is 2
另請參閱
資料流 I/O
_fdopen, _wfdopen
_filelength, _filelengthi64
fopen, _wfopen
freopen, _wfreopen