fgetc 中文man頁面
NAME
fgetc, fgets, getc, getchar, gets, ungetc - 輸入字符和字符串
總覽 (SYNOPSIS)
#include <stdio.h> int fgetc(FILE *stream); char *fgets(char *s, int size, FILE *stream); int getc(FILE *stream); int getchar(void); char *gets(char *s); int ungetc(int c, FILE *stream);
描述 (DESCRIPTION)
fgetc() 從 stream 流 中 讀取 下一個 字符, 然后 從 unsigned char 類型轉(zhuǎn)換 到 int 型 返回, 如果 到達 文件末尾 或 出現(xiàn) 錯誤 則 返回 EOF .
getc() 等于 fgetc() , 只是 它 可能 以 宏 的 形式 實現(xiàn), 并 多次 訪問 stream 流.
getchar() 等于 getc(stdin).
gets() 從 stdin 讀取 一行 字符串, 保存在 s 指向的 緩沖區(qū) 中, 讀到 換行符(newline) 或 EOF 時 操作 結(jié)束, 同時 把 它們 替換為 '\0'. 該函數(shù) 不檢查 緩沖區(qū)溢出 (參見 后面的 BUGS 節(jié)).
fgets() 從 stream 流 中 讀取 多至 size - 1 個 字符, 保存在 s 指向的 緩沖區(qū) 中, 讀到 換行符(newline) 或 EOF 時 操作 結(jié)束, 如果 讀到的 是 換行符, 把 換行符 也保存在 緩沖區(qū) 中. 函數(shù) 將在 ***一個 字符 后面 添加 一個 '\0' 字符.
ungetc() 把 c 轉(zhuǎn)換為 unsigned char 類型 并 回送到 stream 中 供 后續(xù)的 讀操作 讀取. 回送的 所有 字符 將按 相反的 順序 返回; 只保證 一個 回送 操作 的 可靠.
這里 描述的 函數(shù) 可以 混合 使用, 也可以 結(jié)合 stdio 庫中 其他的 輸入函數(shù) 處理 同一個 輸入流.
相應的 無鎖函數(shù)(non-locking) 參見 unlocked_stdio(3).
返回值 (RETURN VALUE)
fgetc(), getc() 和 getchar() 返回 從 unsigned char 類型轉(zhuǎn)換 到 int 型 的 字符, 如果 操作 失敗 或 到達 文件末尾 則 返回 EOF .
如果 操作 成功, gets() 和 fgets() 返回 s 指針, 否則 返回 NULL 指針, 如果 到達 文件末尾 時 還沒有 讀到 字符 也返回 NULL .
操作 成功 時 ungetc() 返回 c , 否則 返回 EOF .
遵循 (CONFORMING TO)
ANSI - C, POSIX.1
BUGS
永遠 不要 使用 gets(). 由于 gets() 事先 不知道 數(shù)據(jù)內(nèi)容, 因此 不可能 知道 應該 讀取 多少 字符, 而且 gets() 會 連續(xù) 接收 字符, 即使 越過 緩沖區(qū) 的 末尾 也 不停止, 所以 這個 函數(shù) 非常危險. 它 曾經(jīng) 被用來 破壞 計算機系統(tǒng) 的 安全. 用 fgets() 代替.
建議 不要 混合 stdio 庫的 輸入函數(shù) 和 低層 read() 函數(shù) 對 輸入流 對應 文件描述符 的 調(diào)用; 其 結(jié)果 沒有 定義, 極可能不是 你 需要的.
另見 (SEE ALSO)
read(2), write(2), ferror(3), fopen(3), fread(3), fseek(3), puts(3), scanf(3), unlocked_stdio(3)
#p#
NAME
fgetc, fgets, getc, getchar, gets, ungetc - input of characters and strings
SYNOPSIS
#include <stdio.h> int fgetc(FILE *stream); char *fgets(char *s, int size, FILE *stream); int getc(FILE *stream); int getchar(void); char *gets(char *s); int ungetc(int c, FILE *stream);
DESCRIPTION
fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error.
getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream more than once.
getchar() is equivalent to getc(stdin).
gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with '\0'. No check for buffer overrun is performed (see BUGS below).
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.
ungetc() pushes c back to stream, cast to unsigned char, where it is available for subsequent read operations. Pushed - back characters will be returned in reverse order; only one pushback is guaranteed.
Calls to the functions described here can be mixed with each other and with calls to other input functions from the stdio library for the same input stream.
For non-locking counterparts, see unlocked_stdio(3).
RETURN VALUE
fgetc(), getc() and getchar() return the character read as an unsigned char cast to an int or EOF on end of file or error.
gets() and fgets() return s on success, and NULL on error or when end of file occurs while no characters have been read.
ungetc() returns c on success, or EOF on error.
CONFORMING TO
ANSI - C, POSIX.1. LSB deprecates gets().
BUGS
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.
It is not advisable to mix calls to input functions from the stdio library with low - level calls to read() for the file descriptor associated with the input stream; the results will be undefined and very probably not what you want.
SEE ALSO
read(2), write(2), ferror(3), fopen(3), fread(3), fseek(3), puts(3), scanf(3), unlocked_stdio(3)