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).
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.
fputc() writes the character c, cast to an unsigned char, to stream.
putc() is equivalent to fputc() except that it may be implemented as a macro which evaluates stream more than once.
查看其实现可以看到其宏定义为#define getc(_fp) _IO_getc (_fp),跟踪代码发现在 libio.h 中 extern int _IO_getc (_IO_FILE *__fp); 是用函数实现的。因而在 linux 中他们两没啥区别。此处详解可以查看引用内容!
getchar() 函数等价于getc(stdin)
ungetc() 将c推回流,强制转换为 unsigned char ,以便后续的读操作使用 c 。
fputc() 将字符 c(强制转换为 unsigned char )写入流
putc()简单意思就是和fputc()函数实现一样,但是使用的时候需要注意宏问题。
putchar(c);等价于putc(c,stdout)
返回值介绍
1 2 3 4
fgetc(), getc() and getchar() return the character read as an unsigned char cast to an int or EOF on end of file or error.
ungetc() returns c on success, or EOF on error.
简单解释:
fgetc(), getc()和 getchar() 读取的字符将其转换为 int 类型进行返回,或者 EOF 或者 error
ungetc() 成功时返回 c ,错误时返回 EOF
fgets和fputs
此函数主要用于字符串的读入与写出,主要以字符串为单位。
函数原型
1 2 3 4 5 6 7 8 9 10 11 12
// focus 重点 char *fgets(char *s, int size, FILE *stream);
char *gets(char *s);
// focus 重点 intfputs(constchar *s, FILE *stream);
intputs(constchar *s);
关于fgets函数参数解释如下:
s : 用于存放数据的字符串指针
size : 最多读取多少个字符
stream : 读取文件
函数介绍
函数描述
1 2 3 4 5 6 7 8 9
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 terminating null byte ('\0') is stored after the last character in the buffer.
gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with a null byte ('\0'). No check for buffer overrun is performed (see BUGS below).
fputs() writes the string s to stream, without its terminating null byte ('\0').
puts() writes the string s and a trailing newline to stdout.
简单解释:
fgets() 从文件中读取最多或少于 size 的字符,并存储到 s 的 buffer 中。读取过程中遇到EOF或换行符结束。
The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.
The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.
On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).
fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.
The scanf() function reads input from the standard input stream stdin, fscanf() reads input from the stream pointer stream, and sscanf() reads its input from the character string pointed to by str.
The vfscanf() function is analogous to vfprintf(3) and reads input from the stream pointer stream using a variable argument list of pointers (see stdarg(3). The vscanf() function scans a variable argu‐ment list from the standard input and the vsscanf() function scans it from a string; these are analogous to the vprintf(3) and vsprintf(3) functions respectively.
The functions in the printf() family produce output according to a format as described below. The functions printf() and vprintf() write output to stdout, the standard output stream; fprintf() and vfprintf() write output to the given output stream; sprintf(), snprintf(), vsprintf() and vsnprintf() write to the character string str.