c standard I/O library

the standard C I/O library:

C标准库提供了文件的标准 I/O 函数库,相比前述的系统调用,主要差别是实现了跨平台的用户态缓冲的解决方案。标准I/O库使用简单,与系统调用I/O相似,也包括打开、读写、关闭这些操作,主要的函数列举如下。

◆ oritened buffering:fwide。
◆ 打开与关闭文件:fopen,freopen, fdopen,  fclose。
◆ 读写文件bianry I/O:fread,fwrite。
◆ 读写文本行:fgets,fputs。
◆ 格式化读:scanf, fscanf,sscanf。
◆ 格式化写:printf, fprintf, dprintf, sprintf, snprintf。
◆ 读写字符:fgetc,getc,getchar,ungetc,fputc,putc,putchar。
◆ positioning stream:ftell, fseek rewind fgetpos, fsetpos 
◆ change the buffering after  open stream : setbuf, setvbuf.
◆ 其他:fflush,fseek , feof, ferror, clearerr 。

所谓标准 I/O 函数实际上是对底层系统调用的包装,最终读写设备或文件的操作仍需调用系统I/O函数来完成。

两个重要的概念:文件指针和流。
标准I/O函数并不直接操作文件描述符,而是使用文件指针。文件指针与文件描述符是一一对应的关系,这种对应关系由标准I/O库 自己内部维护。应用程序调用时,只需要提供文件指针即可。文件指针指向的数据类型为FILE型,但应用程序无须关系它的具体内容。在标准I/O中,一个打开的文件称为流(stream),流可以用于读(输入流)、写(输出流)或者是读写(输入输出流)。每个进程在启动后就会打开三个流,与打开的三个文件相对应:stdin代表标准输入流,stdout代表标准输出流,stderr代表标准错误输出流,它们都是(FILE*)型的指针。标准错误输出流不进行缓冲,输出的内容会马上同步到文件(控制台设备)。

all in:#include <stdio.h>

fwide

int fwide(FILE* stream., int mode) // mode postive can set stream wide oriented or set stream byte oritened zero do nothing , and can not change a oritened stream.

note: no error return ,so sure the stream is right.

fopen, freopen, fdopen:

fopen函数用于打开一个文件流,其原型如下:

FILE *fopen(const char *filename, const char *mode);
FILE *freopen(const char *filename, const char *mode, FILE *special_fp);  // open a file on a special file stream
FILE *fdopen(int fd, const char *type); type: differ from up.        // because some special file cannot be opened with the standard c fopen or freopen

◆ filename:被打开的文件的名称(可包含路径)。
◆ mode:字符串,用于表示打开的模式。
return value:file pointer if ok  NULL on error.

mode如下:
字符串 含义
r 或 rb 以只读方式打开
w 或 wb 以只写方式打开,若文件有内容,则清空
a 或 ab 以只写方式打开,原内容保留,写入的内容附加在文件流尾部
r+ 或 rb+ 或 r+b 以更新方式打开,此时文件可读可写
w+ 或 wb+ 或 w+b 以更新方式打开,文件可读可写,但打开时清空文件内容
a+ 或 ab+ 或 a+b 以更新方式打开,文件可读可写,写入的内容附加在文件流尾部

fclose
fclose 函数用于关闭文件,其原型如下:
int fclose(FILE *stream); // any buffered output data is flushed before the file is closed. any input data that may be buffered is discarded.
return value : 0 if ok EOF on error.

fgetc, getc and getchar:  //(character-at-a-time I/O)
ungetc  
fputc, putc and putchar:

int fgetc(FILE *stream);
int getc(FILE *stream);  ubuntu 有 extern int getc(FILE* stream);   ==> #define getc(_fs)  _IO_GETC( _fs )
int getchar(void);   // is equivalent 
[ɪ'kwɪvələnt] to getc(stdin) === #define getchar() getc(stdin)
int ungetc(int c, FILE* fp); //  c if ok , EOF on error

return value: next character if ok EOF on error 

int fputc(int c, FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c); // is equivalent to putc(c, stdout).

◆ c:是要写入的字符,它虽然是整型,但写入时会将其转换为无符号字符型。
◆ return value: the char convert into integer if ok EOF on error 

fgets and gets:         //(line-at-a-time I/O)
fputs and puts


char *fgets(char *s, int size, FILE *stream);
char *gets(char *s);  // it is unsafe, because we cannot set the size of buffer. And this fuction allow  the buffer to overflow if the line is longer than buffer.  it doesn't store newline sign , as fgets does

◆ s:指向一个缓冲区,用于存放读到的数据。
◆ size:读取的字节数上限,实际读取的字节数不会超过 size-1。
◆ stream:要读取的文件指针。
◆ return value: buffer if ok EOF on error or end of file.


int fputs(cnost char *s, FILE *stream);  // the null byte is not writen at the end of buffer  Note that this need not be line-at-time output because the string donot need a null byte as its llast character.
int puts(const char *s);  // not write null byte and add automatically newline. 

◆ s:要写入的字符串,必须是以 结尾的合法字符串。
◆ stream:要写入的文件指针。
◆ return value: buffer if ok EOF on error.

fflush:
int fflush(FILE *stream);    // flush any unwriten buffering  causing them to be passed to the kernal. if stream = null , all will be flushed. 
return value: 0 if ok EOF on error

I/O error function: ferror, feof

int ferror(FILE *stream); 
int feof(FILE *stream);
void clearerr(FILE* fp);

return value: nozero if condition is ture 0(false) otherwise.

ftell, fseek rewind fgetpos, and fsetpos:

int fseek(FILE *stream, long offset, int whence);
fseek 用于移动文件流的读写位置,其原型如下:
◆ stream:被操作的文件指针。
◆ offset:读写位置的偏移量。
◆ whence:用于指定偏移量的相对启点。
◆ 返回值:0 表示操作成功, -1 表示操作失败并且设置 errno 变量的值为错误码。

whence 参数的取值及含义:
◆ SEEK_SET:表示偏移量相对于文件的开头。
◆ SEEK_CUR:表示偏移量相对于当前的读写位置。
◆ SEEK_END:表示偏移量相对于文件末尾。

long ftell(FILE *fp);  //return current file position indicator -1L on error
如果要将读写位置移动到文件的开头,还可以使用这个函数:
void rewind(FILE *stream);  //return teh beginning of the file 
//the single unix specification:
off_t ftello(FILE * fp);
int fseeko(FILE *fp, off_t offset, int where); // when the offset is not fit in long integer

int fgetpos(FILE *fp, fpos_t *pos);
int fsetpos(FILE *fp, const fpos_t *pos); // return 0 if ok nozero on error

fread and fwrite:(Binary I / O)
size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream);

◆ ptr:指向用于存放读取到的数据的缓冲区。
◆ size:被读取的数据块的长度。
◆ nitems:要读取的数据块的个数。
◆ stream:被读取的文件指针。 
◆ return value:the number of blocks which is read actually。

◆ ptr:指向存放写入数据的缓冲区。
◆ size:要写入的数据块的长度。
◆ nitems:要写入的数据块的个数。
◆ stream:要写入的文件指针。
◆ return value:the number of blocks which is writen actually。

printf, fprintf, dprintf, sprintf and snprintf

int printf(const char *restrict format, ...);
int fprintf(FILE *restrict fp, const char *restrict format, ...);
int dprintf(int fd, const char *restrict format, ...);
All three return: number of characters output if OK, negative value if output error


int sprintf(char *restrict buf, const char *restrict format, ...); // buffer overflows can lead progaram to instability and security violantion.
Returns: number of characters stored in array if OK, negative value if encoding error

int snprintf(char *restrict buf,  size_t n,const char *restrict format, ...);

coversion specifications:
%[flags][fldwide][precision][lemodifier]convtye

setbuf and setvbuf

void setbuf(FILE *restrict fp, char *restrict buf )  //size must be BUFSIZ   
int setvbuf(FILE *restrict fp, char *restrict buf, int modesize_t size)    //mode : _IOFBUF,  _IOLBUF, _IONBUF

Returns: 0 if OK, nonzero on error

Note: must be called after the stream has been opened, but before any other operation is performed on the stream. And we must close the stream before the function return

 scanf, fscanf,sscanf

int scanf(const char *restrict format, ...);
int fscanf(FILE *restrict fp, const char *restrict format, ...);
int sscanf(const char *restrict buf, const char *restrict format, ...);

All three return: number of input items assigned, EOF if input error or end of file before any conversion

 %[*][fldwidth][m][lenmodifier]convtype

 

  The optional leading asterisk is used to suppress conversion. Input is converted as specified by the rest of the conversion specification, but the result is not stored in an argument. 
The fldwidth component specifies the maximum field width in characters. The lenmodifier component specifies the size of the argument to be initialized with the result of the conversion. 
the m character is called assignment-alloction character. It can be used with the %c, %s, and %[ .should use free function to free.

摘录 作者://https://www.cnblogs.com/alionxd/articles/3152455.html

原文地址:https://www.cnblogs.com/yetanghanCpp/p/8820305.html