ungetc 中文man页面

系统
fgetc() 从 stream 流 中 读取 下一个 字符, 然后 从 unsigned char 类型转换 到 int 型 返回, 如果 到达 文件末尾 或 出现 错误 则 返回 EOF .

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 类型转换 到 int 型 返回, 如果 到达 文件末尾 或 出现 错误 则 返回 EOF .

getc() 等于 fgetc() , 只是 它 可能 以 宏 的 形式 实现, 并 多次 访问 stream 流.

getchar() 等于 getc(stdin).

gets()stdin 读取 一行 字符串, 保存在 s 指向的 缓冲区 中, 读到 换行符(newline) 或 EOF 时 操作 结束, 同时 把 它们 替换为 '\0'. 该函数 不检查 缓冲区溢出 (参见 后面的 BUGS 节).

fgets()stream 流 中 读取 多至 size - 1 个 字符, 保存在 s 指向的 缓冲区 中, 读到 换行符(newline) 或 EOF 时 操作 结束, 如果 读到的 是 换行符, 把 换行符 也保存在 缓冲区 中. 函数 将在 ***一个 字符 后面 添加 一个 '\0' 字符.

ungetc()c 转换为 unsigned char 类型 并 回送到 stream 中 供 后续的 读操作 读取. 回送的 所有 字符 将按 相反的 顺序 返回; 只保证 一个 回送 操作 的 可靠.

这里 描述的 函数 可以 混合 使用, 也可以 结合 stdio 库中 其他的 输入函数 处理 同一个 输入流.

相应的 无锁函数(non-locking) 参见 unlocked_stdio(3).

返回值 (RETURN VALUE)

fgetc(), getc() getchar() 返回 从 unsigned char 类型转换 到 int 型 的 字符, 如果 操作 失败 或 到达 文件末尾 则 返回 EOF .

如果 操作 成功, gets()fgets() 返回 s 指针, 否则 返回 NULL 指针, 如果 到达 文件末尾 时 还没有 读到 字符 也返回 NULL .

操作 成功 时 ungetc() 返回 c , 否则 返回 EOF .

遵循 (CONFORMING TO)

ANSI - C, POSIX.1

BUGS

永远 不要 使用 gets(). 由于 gets() 事先 不知道 数据内容, 因此 不可能 知道 应该 读取 多少 字符, 而且 gets() 会 连续 接收 字符, 即使 越过 缓冲区 的 末尾 也 不停止, 所以 这个 函数 非常危险. 它 曾经 被用来 破坏 计算机系统 的 安全. 用 fgets() 代替.

建议 不要 混合 stdio 库的 输入函数 和 低层 read() 函数 对 输入流 对应 文件描述符 的 调用; 其 结果 没有 定义, 极可能不是 你 需要的.

另见 (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)

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-24 16:48:36

man中文man

2011-08-15 10:21:09

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-25 15:39:42

fcloseall中文man

2011-08-25 15:00:15

cfgetispeed中文man

2011-08-19 18:35:50

issue中文man

2011-08-25 17:03:51

pclose中文man

2011-08-25 17:40:25

setvbuf中文man

2011-08-23 14:21:16

poweroff中文man

2011-08-24 15:52:59

intro中文man

2011-08-23 13:40:31

2011-08-25 15:54:08

ferror中文man

2011-08-25 17:24:54

puts中文man

2011-08-23 10:03:40

useradd中文man

2011-08-23 10:29:02

chpasswd中文man

2011-08-23 10:34:22

convertquot中文man

2011-08-23 15:39:34

rpmbuild中文man

2011-08-24 15:48:38

INSERT中文man

2011-08-25 09:40:49

UPDATE中文man
点赞
收藏

51CTO技术栈公众号