string_字符(串)输入输出函数
字符(串)输入输出函数
- 输出函数
#include <stdio.h>
int fputc(int c, FILE *stream);
int fputs(const char *s, FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c);
int puts(const char *s);
注:fputs() writes the string s to stream, without its terminating null byte ('\0').可以连续向流写字符串而一次读出。
puts() writes the string s and a trailing newline to stdout.返回值个数包含‘\n’。
RETURN VALUE
fputc(), putc() and putchar() return the character written as an unsigned char cast to an int or EOF on error.
puts() and fputs() return a nonnegative number(个数) on success, or EOF on error.
- 输入函数
#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);
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
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.
fgets()在缓冲区最后加字符串结束符‘\0’,不越界,比gets()更安全。gets()禁用。两者均是读入一行字符。
返回值:
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.
注:如果一次fgets调用在读入若干个字符后到达文件末尾,则将以读入的字符串加上'\0'存入缓冲区并返回,如果再次调用fgets则返回NULL,可以据此推断是否读到文件未尾。
注:对于fgets来数'\n'是一个特殊字符,而'0'无特殊之处,如果读到'\0'就到普通字符读入。如果文件中存在'\0',调用fgets之后就无法判断缓冲区中'\0'究竟是从文件读上来的还是fgets自动添加的结束符。所以fgets只适合读文本文件而不适合读二进制文件,并且文本文件中所有的字符都应该是可见字符,不能是'\0'.
注:fgets空间小时‘\0’占用最后一个字符,其余舍弃;空间大时若有'\n',读入‘\n’。
gets空间小时有时可正常输出(不检查越界),异常情况自动加‘\0’;空间大时有'\n’,不读入‘\n’,不换行。
- scanf
int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *str, const char *format, ...);
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 format string consists of a sequence of directives which describe how to process the sequence of input characters. If processing of a directive fails, no further input is read, and scanf() returns. A "failure" can be either of the following: input failure, meaning that input characters were unavailable, or matching failure, meaning that the input was inappropriate.
返回值:
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.
scanf()遇到white-sapce字符即结束,white-space: space(空格), form-feed(\f), newline(\n), carriage return(\r), horizontal tab(\t), and vertical tab(\v).
注:scanf只接收输入,不能输出。
- printf
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
The functions snprintf() and vsnprintf() write at most size bytes (including the terminating null byte ('\0')) to str.
Return value
Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).
The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated.
If an output error is encountered, a negative value is returned.
注:EOF = -1
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 几个技巧,教你去除文章的 AI 味!
· 系统高可用的 10 条军规
· 关于普通程序员该如何参与AI学习的三个建议以及自己的实践
· 对象命名为何需要避免'-er'和'-or'后缀
· AI与.NET技术实操系列(八):使用Catalyst进行自然语言处理