C/C++ I/O库函数
来源:互联网 发布:c 数组中括号位置 编辑:程序博客网 时间:2024/05/16 00:38
转自:http://blog.csdn.net/isgray/article/details/6838341
头文件:stdio.h
函数列表:
文件操作
remove删除文件rename重命名文件tmpfile打开一个临时文件tmpnam产生临时文件名
文件访问
fclose
关闭文件
fflush
刷新流
fopen
打开文件
freopen
用不同的文件或模式打开流
setbuf
设置流缓冲
setvbuf
改变流缓存
格式化输入/输出
fprintf写格式化好的输出到流fscanf从流中读取格式化好的数据printf打印格式化好的数据到stdoutscanf从stdin读取格式化好的数据sprintf写格式化好的数据到字符串sscanf从字符串中读取格式化好的数据vfprintf写格式化好的变量参数列表到流vprintf写格式化好的变量参数列表到stdoutvsprintf写格式化好的变量参数列表到字符串
字符输入/输出
fgetc从流中获取字符fgets从流中获取字符串fputc写字符到流fputs写字符串到流getc从流中获取字符getchar从stdin获取字符gets从stdin获取字符串putc写字符到流putchar写字符到stdoutputs写字符串到stdoutungetc字符退回到流
直接输入/输出
fread从流中读取一块数据fwrite写一块数据到流
文件定位
fgetpos得到在流中的当前位置fseek重新定位流的位置fsetpos设置流的位置ftell得到在流中的当前位置rewind设置位置到开始位置
错误处理
clearerr清楚错误指示器feof检查eof指示器ferror检查错误指示器perror输出错误信息
宏
EOF文件结束符FILENAME_MAX文件名的最大长度NULLNull指针TMP_MAX临时文件的数量
类型
FILE包含空置一个流的信息的对象fpos_t包含指定一个文件位置的信息的对象size_tunsigned intergral type
函数说明:
1、remove
原型:int remove (const char *filename);
说明:删除filename指定的文件
返回值:成功,返回0;否则,返回错误码。
2、rename
原型:int rename (const char * oldname, const char *newname);
说明:改变文件的名字,由oldname变成newname。如果oldname和newname指示了不同的路径,并且系统支持这一点,文件就好被移动到新的位置。
返回值:成功,返回0;否则,返回错误码。
3、tmpfile
原型:FILE * tmpfile ();
说明:创建一个临时二进制文件,文件名要不同于任何已存在的文件。当流关闭(fclose)的时候,或是当程序正常终止的时候,创建的临时文件自动被删除。
返回值:成功,返回一个指向临时文件的流指针;否则,返回NULL指针。
4、tmpnam
原型:char * tmpnam( char * str);
说明:产生一个包含不同于任何现有文件名不同的文件名的字符串。这个字符串能够用来产生一个临时文件,而没有重写任何现有的文件。如果参数str为null指针,则结果字符
串存储在一个内部静态(static)数组中,该数组可以通过tmpnam返回的指针访问。当下次调用函数的时候(参数为null指针),该内存(静态数组)被修改。如果参数str不
是一个null指针,则str必须指向一个至少有L_tmpnam个字节的内存。这个函数返回的文件名,通过调用fopen,可以用来创建普通的临时文件。用这种方式创建的文件,不同
于用tmpfile创建的临时文件,在结束的时候不会自动地被删除,应该通过调用remove来删除。
返回值:成功,如果str不是null,返回str,如果是null,返回一个字符串;失败,返回null。
5、fclose
原型:int fclose (FILE * stream);
说明:关闭stream关联的文件,并将这种关联解除。stream关联的所有内部缓冲区被刷新,任何没有写的缓存内容被写出来,任何没有读的缓存内容被丢弃。即使调用失败了
,stream就不再与这个文件想关联。
返回值:成功,返回0;失败,返回EOF。
6、fflush
原型:int fflush (FILE *stream);
说明:如果参数stream是为写而打开的,最后一次i/o操作是输出操作,那么输出缓存中的未写的数据被写入到这个文件中。如果参数stream是为读而打开的,最后一次i/o操作
是输入操作,则函数的行为取决于特定的库实现。如果stream是null指针,则所有打开的文件被刷新。在调用之后,stream保持打开。当一个文件被关闭,或者是因为调用fclose,或者是因为程序终结,所有与之关联的缓存区被自动地刷新。
返回值:成功,返回0;否则,返回EOF,错误指示器被设置。
7、fopen
原型:FILE * fopen (const char *filename, const char *mode);
说明:打开文件,文件的名字由filename指定,并把这个文件同一个流想关联;mode参数指定文件的打开方式。
mode的取值
"r" 或"rb"只读方式打开"w"或"wb"只写方式打开,并把文件长度截短为零"a"或"ab"写方式打开,新内容追加在文件尾"r+"或"rb+"或"r+b"修改方式打开(读和写)"w+"或"wb+"或"w+b"修改方式打开,并把文件长度截短为零"a+"或"ab+"或"a+b"修改方式打开,新内容追加在文件尾字母b表示是一个二进制文件,而不是文本文件,unix和linux把所有文件都看作是二进制文件。
返回值:成功,返回一个FILE *指针,指向打开的文件;否则,返回NULL。
8、freopen
原型:FILE * freopen(const char *filename,const char * mode, FILE * stream);
说明:首先关闭同stream参数关联的文件,并解除关联。无论是否关闭成功,freopen打开filename参数指定的文件,并将这个文件同一个指定的流想关联。这个函数对于将已
定义好的流重定向到特定的文件是很有用的。mode参数同fopen。
返回值:成功,返回一个指向流的指针;否则,返回null指针。
9、setbuf
原型:void setbuf (FILE *stream, char *buffer);
说明:将buffer用于stream流的I/O操作,使得stream成为一个全缓存流。如果buffer是null指针,就禁止stream流缓存,使得stream成为一个不可缓存的流。这个函数应该在
stream流关联的文件被打开,还没有进行输入输出操作之前被调用。buffer必须指向一个至少有BUFSIZ字节的内存区。在全缓存流模式下,写操作不会直接写到与之关联的
设备中,数据在buffer中累积,当buffer满时,这些数据作为一个块(block)被写入到设备中。这种整个block的写操作可以通过刷新(flush)流实现,即调用fflush和fclose。
当程序结束时,所有的缓存区被刷新。在非缓存流模式下,数据被直接写入到物理设备中。所有的文件都是用默认分配的缓存打开的。setbuf函数能够用来定义一个用户定义的
缓存,或者禁用一个特定流的缓存。系统标准流,例如stdout和stderr,默认下是不可缓存的,除非被重定向过。如果要改变一个线性缓存流,使用setvbuf。
返回值:没有。
10、setvbuf
原型:int setvbuf (FILE *stream, char * buffer, int mode, size_t size);
说明:改变buffer,使之能够用于stream流的I/O操作。这个函数允许指定流的mode和size。这个函数应该在stream流关联的文件被打开,还没有进行输入输出操作之前被调
用。缓存区buffer的大小由size参数指定,单位字节。如果没有指定流(例如,buffer为null),系统将动态地分配size字节的内存作为流stream的缓存。参数mode用来指定缓
存buffer的模式:全缓存,线性缓存,非缓存。在全缓存模式下,写操作不直接写入到物理设备,数据暂存在buffer中,直到buffer满,作为一个block写入到设备中。这种写操
作可以通过flush流来强制实行,例如调用fflush和fclose。在线性缓存流模式下,每次一个换行符被写的时候,buffer被写入到设备中。在非缓存流模式下,数据被直接写入到
设备中。所有的文件都是用默认分配的缓存打开的。setvbuf函数能够用来重定义buffer的大小,定义一个用户定义的缓存,或者禁用一个特定流的缓存。系统标准流,例如
stdout和stderr,默认下是不可缓存的,除非被重定向过。
参数取值:mode
_IOFBF全缓存。输出,当buffer满时,数据被写;输入,buffer是空的,写入buffer_IOLBF线性缓存。输出,当一个换行符被插入到流中,或者buffer满,数据被写入设备;输入,当buffer是空的,写操作被请求,数据被写入buffer中直到下一个新的换行符_IONBF非缓存。不使用缓存区,忽略size和buffer两个参数返回值:成功,返回0;否则,返回非0。
11、fprintf
原型:int fprintf (FILE *stream, const char *format, ...);
说明:将format参数指定的格式的数据写到stream参数流中。
参数:stream,指向FILE对象的指针。
format,c字符串,包含了要写入到流中的文本。格式标签的格式如下:
%[flags] [width] [.precision] [length] specifier
specifieroutputExamplec字符ad or i有符号的十进制整数392e科学计数法,使用e字符3.9265e+2E科学计数法,使用E字符3.9265E+2f十进制浮点数392.65g使用%e,%f的短格式392.65G使用%E,%f的短格式392.65o有符号的八进制数610s字符串sampleu无符号的十进制整数7235x无符号的十六进制整数7faX无符号的十六进制整数7FAp指针地址B800:0000n什么都不输出。必须是一个指向signed int 的指针 %跟在%后面的%被写入到流中
flags
description
-
在给定的宽带内做对齐;默认是右对齐
+
强迫结果带上符号(+/-);默认负数有-号,正数没有+号
(space)
如果没有符号要写,就在值的前面插入空白字符
#
同o,x或X使用,结果的前面加上0,0x或0X;
同e或E,f使用,迫使结果包含一个十进制点(.),哪怕后面没有数字出现;
同g或G使用,结果同e或E相同,但是后补0不会被移除
0
右对齐时,用0填充剩余的部分
widthdescription(nubmer)被打印出的字符最小数目。如果要打印的结果的长度比number小,则结果补空白;即使结果长度比number大,值不会被截断*可变宽带,没有在format字符串里给出,在格式化的参数之前的一个额外的整型参数给出
.precisiondescription.number
对于整型specifier(d,i,o,u,x,X),precision指定了有效数字。如果值的有效数字少,则补0,如果值的有效数字多,则结果不截断。0意味着,对于值0没有字符会被写。
对于e,E,f 这些specifers,number指出了小数点后的有效数字。
对于个g,G这些specifiers,number指出了有效数字的最大位数。
对于s,是被打印出来的字符的最大数量。
对于c,没有影响。
当没有指定precision时,默认值是1;如果这个字段没用明确的值指定,则假定是0
.*可变精度,精度没有在format字符串中给出,而是在后续参数中给出,这个参数位于那些被format格式化的参数之前lengthdescriptionh参数被理解为short int 或 unsigned short int (仅适应于i,d,o,u,x和X)l(小写L)
参数被理解为long int 或 unsigned long int (适应于i,d,o,u,x和X);
或者理解为wide character(宽字符)或 wide character string(宽字符串)(适应于c和s)
L参数被理解为long double (仅适应于浮点specifiers:e,E,f,g 和 G)
12、fscanf
原型:int fscanf (FILE *stream, const char *format, ...);
参数:stream,指向文件流的指针。
format,格式化字符串。格式标签的格式如下:
%[*][width][modifiers]type
h:short int (适用于d,i,n)或者 unsigned short int(适用于o,u,x);
l:long int (适用于d,i,n)或者 unsigned long int (适用于o,u,x)或者 double (适用于e,f,g);
L:long double(适用于e,f,g)
type见下表typeQualifying InputType of argumentc单个字符:读取文本字符。如果width不是1,则读取width个字符,并存储。null字符不添加到结尾char *d十进制整数:有+,-号int *e,E,f,g,G浮点数:float *o八进制数int *s字符串:读取一系列字符,直到遇到空白字符char *u无符号十进制整数unsigned int *x,X十六进制整数int *
说明:从stream中读取数据,根据format参数解析数据,并存储在额外的参数指定的位置。
返回值:成功,返回读取项目的数量;否则,返回EOF。
13、printf
原型:int printf (const char *format, ...);
参数:format,格式化字符串。
format格式如下:
%[flags][width][.precision][length]specifier
Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.0Left-pads the number with zeroes (0) instead of spaces, where padding is specified (seewidth sub-specifier).
For e, E and f specifiers: this is the number of digits to be printedafter the decimal point.
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
For c type: it has no effect.
When no precision is specified, the default is 1. If the period is specified without an explicit value forprecision,0 is assumed..*The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
说明:向stdout写数据,这些数据由format格式化。format之后的参数个数要与format中说明的一致。
返回值:成功,返回已写字符的个数;否则,返回一个负数。
14、scanf
原型:int scanf (const char *format, ...);
参数:format,格式化字符串。
格式如下:
%[*][width][modifiers]type
h : short int (for d, i and n), orunsigned short int (foro,u and x)
l : long int (for d, i and n), orunsigned long int (foro,u and x), or double (fore, f andg)
L : long double (for e, f and g)typeA character specifying the type of data to be read and how it is expected to be read. See next table.
scanf type specifiers:
说明: 从stdin读取数据,根据format进行格式化,并将结果存储在随后的参数指定的位置。
15、sprintf
原型:int sprintf (char *str, const char *format, ...);
说明:将格式化的数据写入到字符串str中。
参数:str,存储格式化好的数据。
format,格式化字符串。格式如下:
%[flags][width][.precision][length]specifier
The tag can also contain flags, width, .precision and modifiers sub-specifiers, which are optional and follow these specifications:
Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.0Left-pads the number with zeroes (0) instead of spaces, where padding is specified (seewidth sub-specifier).
For e, E and f specifiers: this is the number of digits to be printedafter the decimal point.
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
For c type: it has no effect.
When no precision is specified, the default is 1. If the period is specified without an explicit value forprecision,0 is assumed..*The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
返回值:成功,返回已写数据的个数;否则,返回一个负数。
16、sscanf
说明:从字符串(缓冲区)里读取数据,格式后存在参数指定的内存区域。
原型:int sscanf ( const char * str, const char * format, ...);
参数:str,要读取数据的字符串指针。
format,格式化字符串指针。格式化字符串格式如下:
%[*][width][modifiers]type
h : short int (for d, i and n), orunsigned short int (for o, u and x)
l : long int (for d, i and n), orunsigned long int (for o, u and x), or double (fore, f and g)
L : long double (for e, f and g)typeA character specifying the type of data to be read and how it is expected to be read. See next table.
sscanf type specifiers:
返回值:成功,返回变量被写的个数;失败,返回EOF。
17、vfprintf
说明:将参数经过格式化后写入流中。
原型:int vfprintf ( FILE * stream, const char * format, va_list arg );
参数:stream,要写入的流指针。
format,格式字符串指针。
arg,参数列表。
format格式如下:
%[flags][width][.precision][length]specifier
The tag can also contain flags, width, .precision and modifiers sub-specifiers, which are optional and follow these specifications:
Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.0Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see widthsub-specifier).
For e, E and f specifiers: this is the number of digits to be printed after de decimal point.
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
For c type: it has no effect.
When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed..*The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
返回值:成功,返回已写字符的个数;失败,返回负数。
18、vprintf
说明:将参数进过格式化后写入到stdout中。
原型:int vprintf ( const char * format, va_list arg );
参数:format,格式化字符串指针。
arg,可变参数列表。
format格式:
%[flags][width][.precision][length]specifier
The tag can also contain flags, width, .precision and modifiers sub-specifiers, which are optional and follow these specifications:
Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.0Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see widthsub-specifier).
For e, E and f specifiers: this is the number of digits to be printed after de decimal point.
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
For c type: it has no effect.
When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed..*The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
返回值:成功,返回已写字符的个数;失败,返回负数。
19、vsprintf
说明:将参数列表格式后写入字符串。
原型:int vsprintf (char * str, const char * format, va_list arg );
参数:str,要写入的字符串指针。
format,格式化字符串指针。
arg,可变参数列表。
format格式如下:
%[flags][width][.precision][length]specifier
The tag can also contain flags, width, .precision and modifiers sub-specifiers, which are optional and follow these specifications:
Used with e, E and f, it forces the written output to contain a decimal point even if no digits would follow. By default, if no digits follow, no decimal point is written.
Used with g or G the result is the same as with e or E but trailing zeros are not removed.0Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see widthsub-specifier).
For e, E and f specifiers: this is the number of digits to be printed after de decimal point.
For g and G specifiers: This is the maximum number of significant digits to be printed.
For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.
For c type: it has no effect.
When no precision is specified, the default is 1. If the period is specified without an explicit value for precision, 0 is assumed..*The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
返回值:成功,返回已写字符的个数;失败,返回负数。
- C/C++ I/O库函数
- C/C++ I/O库函数
- c/c++ I/O库函数
- C标准I/O库函数与无缓存的I/O函数
- 文件I/O之C标准库函数和系统库函数区别
- C++I/O操作
- c++I/O流
- 标准 C I/O
- C++I/O技术
- c++I/O操作
- C之I/O
- C标准I/O
- 标准 C I/O
- 标准C I/O
- objective-c I/O
- C++I/O输入输出
- 文件I/O编程学习笔记二(c语言库函数访问文件)
- C语言文件读写标准I/O库函数的相关操作
- JAVA工程师成长之路要学习的技术视频和实战项目(超全)
- Ubuntu源配置文件source.list 格式解释
- 手把手教你通过Eclipse工程配置调用JNI完全攻略
- 虚拟机异构环境迁移
- C++操作符重载
- C/C++ I/O库函数
- JS编码解码
- 日语学习之沪江N4基础 20141127 -5
- VB.NET 读取写入XML文件
- java23种常用设计模式之装饰模式(Decorator)
- linux sudo配置
- mysql 常用命令
- NServiceBus
- C语言函数 memset