字符(串)输入输出函数

字符(串)输入输出函数

  • 输出函数

#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 ('').可以连续向流写字符串而一次读出。

puts() writes the string s and a trailing newline to stdout.返回值个数包含‘ ’。

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 (''). 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 ('') is stored after the last character in the buffer.

fgets()在缓冲区最后加字符串结束符‘’,不越界,比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调用在读入若干个字符后到达文件末尾,则将以读入的字符串加上''存入缓冲区并返回,如果再次调用fgets则返回NULL,可以据此推断是否读到文件未尾。

注:对于fgets来数' '是一个特殊字符,而'0'无特殊之处,如果读到''就到普通字符读入。如果文件中存在'',调用fgets之后就无法判断缓冲区中''究竟是从文件读上来的还是fgets自动添加的结束符。所以fgets只适合读文本文件而不适合读二进制文件,并且文本文件中所有的字符都应该是可见字符,不能是''.

注:fgets空间小时‘’占用最后一个字符,其余舍弃;空间大时若有' ',读入‘ ’。

gets空间小时有时可正常输出(不检查越界),异常情况自动加‘’;空间大时有' ’,不读入‘ ’,不换行。

  • 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( ), carriage return( ), horizontal tab( ), 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 ('')) 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 ('')). 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

原文地址:https://www.cnblogs.com/embedded-linux/p/5027496.html