C/C++ I/O库函数

头文件:stdio.h

函数列表:

 

文件操作

remove

删除文件

rename

重命名文件

tmpfile

打开一个临时文件

tmpnam

产生临时文件名

 

文件访问

fclose

关闭文件

fflush

刷新流

fopen

打开文件

freopen

用不同的文件或模式打开流

setbuf

设置流缓冲

setvbuf

改变流缓存

 

格式化输入/输出

fprintf

写格式化好的输出到流

fscanf

从流中读取格式化好的数据

printf

打印格式化好的数据到stdout

scanf

stdin读取格式化好的数据

sprintf

写格式化好的数据到字符串

sscanf

从字符串中读取格式化好的数据

vfprintf

写格式化好的变量参数列表到流

vprintf

写格式化好的变量参数列表到stdout

vsprintf

写格式化好的变量参数列表到字符串

 

字符输入/输出

fgetc

从流中获取字符

fgets

从流中获取字符串

fputc

写字符到流

fputs

写字符串到流

getc

从流中获取字符

getchar

stdin获取字符

gets

stdin获取字符串

putc

写字符到流

putchar

写字符到stdout

puts

写字符串到stdout

ungetc

字符退回到流

 

直接输入/输出

fread

从流中读取一块数据

fwrite

写一块数据到流

 

文件定位

fgetpos

得到在流中的当前位置

fseek

重新定位流的位置

fsetpos

设置流的位置

ftell

得到在流中的当前位置

rewind

设置位置到开始位置

 

错误处理

clearerr

清楚错误指示器

feof

检查eof指示器

ferror

检查错误指示器

perror

输出错误信息

 

EOF

文件结束符

FILENAME_MAX

文件名的最大长度

NULL

Null指针

TMP_MAX

临时文件的数量

 

类型

FILE

包含空置一个流的信息的对象

fpos_t

包含指定一个文件位置的信息的对象

size_t

unsigned  intergral  type

 

 

 

 

函数说明:

1remove

原型:int  remove(const  char  *filename);

说明:删除filename指定的文件

返回值:成功,返回0;否则,返回错误码。

 

2rename

原型:int  rename(const  char  *  oldname,  const  char *newname);

说明:改变文件的名字,由oldname变成newname。如果oldnamenewname指示了不同的路径,并且系统支持这一点,文件就好被移动到新的位置。

返回值:成功,返回0;否则,返回错误码。

 

3tmpfile

原型:FILE * tmpfile ();

说明:创建一个临时二进制文件,文件名要不同于任何已存在的文件。当流关闭(fclose)的时候,或是当程序正常终止的时候,创建的临时文件自动被删除。

返回值:成功,返回一个指向临时文件的流指针;否则,返回NULL指针。

 

4tmpnam

原型:char * tmpnam( char  * str);

说明:产生一个包含不同于任何现有文件名不同的文件名的字符串。这个字符串能够用来产生一个临时文件,而没有重写任何现有的文件。如果参数strnull指针,则结果字符

串存储在一个内部静态(static)数组中,该数组可以通过tmpnam返回的指针访问。当下次调用函数的时候(参数为null指针),该内存(静态数组)被修改。如果参数str

是一个null指针,则str必须指向一个至少有L_tmpnam个字节的内存。这个函数返回的文件名,通过调用fopen,可以用来创建普通的临时文件。用这种方式创建的文件,不同

于用tmpfile创建的临时文件,在结束的时候不会自动地被删除,应该通过调用remove来删除。

返回值:成功,如果str不是null,返回str,如果是null,返回一个字符串;失败,返回null

 

5fclose

原型:int  fclose(FILE  * stream);

说明:关闭stream关联的文件,并将这种关联解除。stream关联的所有内部缓冲区被刷新,任何没有写的缓存内容被写出来,任何没有读的缓存内容被丢弃。即使调用失败了

stream就不再与这个文件想关联。

返回值:成功,返回0;失败,返回EOF

 

6fflush

原型:int  fflush(FILE  *stream);

说明:如果参数stream是为写而打开的,最后一次i/o操作是输出操作,那么输出缓存中的未写的数据被写入到这个文件中。如果参数stream是为读而打开的,最后一次i/o操作

是输入操作,则函数的行为取决于特定的库实现。如果streamnull指针,则所有打开的文件被刷新。在调用之后,stream保持打开。当一个文件被关闭,或者是因为调用fclose,或者是因为程序终结,所有与之关联的缓存区被自动地刷新。

返回值:成功,返回0;否则,返回EOF,错误指示器被设置。

 

7fopen

原型: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表示是一个二进制文件,而不是文本文件,unixlinux把所有文件都看作是二进制文件。

返回值:成功,返回一个FILE *指针,指向打开的文件;否则,返回NULL

 

8freopen

原型:FILE * freopen(const char *filename,const char * mode, FILE * stream);

说明:首先关闭同stream参数关联的文件,并解除关联。无论是否关闭成功,freopen打开filename参数指定的文件,并将这个文件同一个指定的流想关联。这个函数对于将已

定义好的流重定向到特定的文件是很有用的。mode参数同fopen

返回值:成功,返回一个指向流的指针;否则,返回null指针。

 

9setbuf

原型:void setbuf (FILE *stream, char *buffer);

说明:将buffer用于stream流的I/O操作,使得stream成为一个全缓存流。如果buffernull指针,就禁止stream流缓存,使得stream成为一个不可缓存的流。这个函数应该在

stream流关联的文件被打开,还没有进行输入输出操作之前被调用。buffer必须指向一个至少有BUFSIZ字节的内存区。在全缓存流模式下,写操作不会直接写到与之关联的

设备中,数据在buffer中累积,当buffer满时,这些数据作为一个块(block)被写入到设备中。这种整个block的写操作可以通过刷新(flush)流实现,即调用fflushfclose

当程序结束时,所有的缓存区被刷新。在非缓存流模式下,数据被直接写入到物理设备中。所有的文件都是用默认分配的缓存打开的。setbuf函数能够用来定义一个用户定义的

缓存,或者禁用一个特定流的缓存。系统标准流,例如stdoutstderr,默认下是不可缓存的,除非被重定向过。如果要改变一个线性缓存流,使用setvbuf

返回值:没有。

 

10setvbuf

原型:int setvbuf (FILE *stream, char * buffer, int mode, size_t size);

说明:改变buffer,使之能够用于stream流的I/O操作。这个函数允许指定流的modesize。这个函数应该在stream流关联的文件被打开,还没有进行输入输出操作之前被调

用。缓存区buffer的大小由size参数指定,单位字节。如果没有指定流(例如,buffernull),系统将动态地分配size字节的内存作为流stream的缓存。参数mode用来指定缓

buffer的模式:全缓存,线性缓存,非缓存。在全缓存模式下,写操作不直接写入到物理设备,数据暂存在buffer中,直到buffer满,作为一个block写入到设备中。这种写操

作可以通过flush流来强制实行,例如调用fflushfclose。在线性缓存流模式下,每次一个换行符被写的时候,buffer被写入到设备中。在非缓存流模式下,数据被直接写入到

设备中。所有的文件都是用默认分配的缓存打开的。setvbuf函数能够用来重定义buffer的大小,定义一个用户定义的缓存,或者禁用一个特定流的缓存。系统标准流,例如

stdoutstderr,默认下是不可缓存的,除非被重定向过。

参数取值:mode

_IOFBF

全缓存。输出,当buffer满时,数据被写;输入,buffer是空的,写入buffer

_IOLBF

线性缓存。输出,当一个换行符被插入到流中,或者buffer满,数据被写入设备;输入,当buffer是空的,写操作被请求,数据被写入buffer中直到下一个新的换行符

_IONBF

非缓存。不使用缓存区,忽略sizebuffer两个参数

返回值:成功,返回0;否则,返回非0

 

11fprintf

原型:int fprintf (FILE *stream, const char *format, ...);

说明:将format参数指定的格式的数据写到stream参数流中。

参数:stream,指向FILE对象的指针。

           formatc字符串,包含了要写入到流中的文本。格式标签的格式如下:

           %[flags] [width] [.precision] [length] specifier

           

specifier

output

Example

c

字符

a

d or i

有符号的十进制整数

392

e

科学计数法,使用e字符

3.9265e+2

E

科学计数法,使用E字符

3.9265E+2

f

十进制浮点数

392.65

g

使用%e,%f的短格式

392.65

G

使用%E,%f的短格式

392.65

o

有符号的八进制数

610

s

字符串

sample

u

无符号的十进制整数

7235

x

无符号的十六进制整数

7fa

X

无符号的十六进制整数

7FA

p

指针地址

B800:0000

n

什么都不输出。必须是一个指向signed int 的指针

 

%

跟在%后面的%被写入到流中

 

 

flags

description

-

在给定的宽带内做对齐;默认是右对齐

+

强迫结果带上符号(+/-);默认负数有-号,正数没有+

(space)

如果没有符号要写,就在值的前面插入空白字符

#

oxX使用,结果的前面加上0,0x0X

eEf使用,迫使结果包含一个十进制点(.),哪怕后面没有数字出现;

gG使用,结果同eE相同,但是后补0不会被移除

0

右对齐时,用0填充剩余的部分

 

width

description

nubmer

被打印出的字符最小数目。如果要打印的结果的长度比number小,则结果补空白;即使结果长度比number大,值不会被截断

*

可变宽带,没有在format字符串里给出,在格式化的参数之前的一个额外的整型参数给出

 

.precision

description

.number

对于整型specifierdiouxX),precision指定了有效数字。如果值的有效数字少,则补0,如果值的有效数字多,则结果不截断。0意味着,对于值0没有字符会被写。

对于eEf 这些specifersnumber指出了小数点后的有效数字。

对于个gG这些specifiersnumber指出了有效数字的最大位数。

对于s,是被打印出来的字符的最大数量。

对于c,没有影响。

当没有指定precision时,默认值是1;如果这个字段没用明确的值指定,则假定是0

.*

可变精度,精度没有在format字符串中给出,而是在后续参数中给出,这个参数位于那些被format格式化的参数之前

 

length

description

h

参数被理解为short int unsigned short int (仅适应于idouxX

l(小写L

参数被理解为long int unsigned long int (适应于idouxX);

或者理解为wide character(宽字符)或 wide character string(宽字符串)(适应于cs

L

参数被理解为long double (仅适应于浮点specifierse,E,f,g G

 

12fscanf

原型:int  fscanf(FILE *stream, const char *format, ...);

参数:stream,指向文件流的指针。

           format,格式化字符串。格式标签的格式如下:

           %[*][width][modifiers]type

            

*

从流中读取的数据被忽略,不会存储在相应的变量里

width

给出了当前读操作中要读取的最大字符数

modefiers

hshort int (适用于din)或者 unsigned short int(适用于oux);

llong int (适用于din)或者 unsigned long int (适用于oux)或者 double (适用于efg);

Llong double(适用于efg

type

见下表

 

type

Qualifying Input

Type of argument

c

单个字符:读取文本字符。如果width不是1,则读取width个字符,并存储。null字符不添加到结尾

char *

d

十进制整数:有+-

int *

e,E,f,g,G

浮点数:

float *

o

八进制数

int *

s

字符串:读取一系列字符,直到遇到空白字符

char *

u

无符号十进制整数

unsigned int *

xX

十六进制整数

int *

说明:从stream中读取数据,根据format参数解析数据,并存储在额外的参数指定的位置。

返回值:成功,返回读取项目的数量;否则,返回EOF

 

13printf

原型:int printf(const char *format, ...);

参数:format,格式化字符串。

           format格式如下:

           %[flags][width][.precision][length]specifier 
          

specifier

Output

Example

c

Character

a

d or i

Signed decimal integer

392

e

Scientific notation (mantise/exponent) using e character

3.9265e+2

E

Scientific notation (mantise/exponent) using E character

3.9265E+2

f

Decimal floating point

392.65

g

Use the shorter of %e or %f

392.65

G

Use the shorter of %E or %f

392.65

o

Unsigned octal

610

s

String of characters

sample

u

Unsigned decimal integer

7235

x

Unsigned hexadecimal integer

7fa

X

Unsigned hexadecimal integer (capital letters)

7FA

p

Pointer address

B800:0000

n

Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.

 

%

A % followed by another % character will write % tostdout.

%

 

flags

description

-

Left-justify within the given field width; Right justification is the default (seewidth sub-specifier).

+

Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a- sign.

(space)

If no sign is going to be written, a blank space is inserted before the value.

#

Used with o, x or X specifiers the value is preceeded with0,0x or0X respectively for values different than zero.
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.

0

Left-pads the number with zeroes (0) instead of spaces, where padding is specified (seewidth sub-specifier).

 

width

description

(number)

Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

*

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

 

.precision

description

.number

For integer specifiers (d, i, o, u, x,X):precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. Aprecision of0 means that no character is written for the value0.
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.

 

length

description

h

The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers:i,d,o, u, x and X).

l

The argument is interpreted as a long int or unsigned long int for integer specifiers (i,d,o,u, xand X), and as a wide character or wide character string for specifiersc ands.

L

The argument is interpreted as a long double (only applies to floating point specifiers:e,E,f, g andG).

说明:向stdout写数据,这些数据由format格式化。format之后的参数个数要与format中说明的一致。

返回值:成功,返回已写字符的个数;否则,返回一个负数。

 

14scanf

原型:int  scanf(const char *format, ...);

参数:format,格式化字符串。

           格式如下:

          %[*][width][modifiers]type

*

An optional starting asterisk indicates that the data is to be retrieved fromstdin but ignored, i.e. it is not stored in the corresponding argument.

width

Specifies the maximum number of characters to be read in the current reading operation

modifiers

Specifies a size different from int (in the case of d, i and n), unsigned int (in the case of o, uandx) orfloat (in the case ofe, f and g) for the data pointed by the corresponding additional argument:
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)

type

A character specifying the type of data to be read and how it is expected to be read. See next table.


scanf type specifiers:

type

Qualifying Input

Type of argument

c

Single character: Reads the next character. If a width different from 1 is specified, the function readswidth characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end.

char *

d

Decimal integer: Number optionally preceded with a + or- sign.

int *

e,E,f,g,G

Floating point: Decimal number containing a decimal point, optionally preceded by a+or- sign and optionally folowed by thee or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4

float *

o

Octal integer.

int *

s

String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).

char *

u

Unsigned decimal integer.

unsigned int *

x,X

Hexadecimal integer.

int *

说明:stdin读取数据,根据format进行格式化,并将结果存储在随后的参数指定的位置。

 

15sprintf

原型:int sprintf (char *str, const char *format, ...);

说明:将格式化的数据写入到字符串str中。

参数:str,存储格式化好的数据。

           format,格式化字符串。格式如下:

            %[flags][width][.precision][length]specifier 
           

specifier

Output

Example

c

Character

a

d or i

Signed decimal integer

392

e

Scientific notation (mantise/exponent) using e character

3.9265e+2

E

Scientific notation (mantise/exponent) using E character

3.9265E+2

f

Decimal floating point

392.65

g

Use the shorter of %e or %f

392.65

G

Use the shorter of %E or %f

392.65

o

Signed octal

610

s

String of characters

sample

u

Unsigned decimal integer

7235

x

Unsigned hexadecimal integer

7fa

X

Unsigned hexadecimal integer (capital letters)

7FA

p

Pointer address

B800:0000

n

Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.

 

%

A % followed by another % character will write % to the string.


The tag can also contain
 flags, width, .precision and modifiers sub-specifiers, which are optional and follow these specifications:

flags

description

-

Left-justify within the given field width; Right justification is the default (seewidth sub-specifier).

+

Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a- sign.

(space)

If no sign is going to be written, a blank space is inserted before the value.

#

Used with o, x or X specifiers the value is preceeded with0,0x or 0X respectively for values different than zero.
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.

0

Left-pads the number with zeroes (0) instead of spaces, where padding is specified (seewidth sub-specifier).

 

width

description

(number)

Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

*

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

 

.precision

description

.number

For integer specifiers (d, i, o, u, x,X):precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. Aprecision of0 means that no character is written for the value0.
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.

 

length

description

h

The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers:i,d, o, u, x and X).

l

The argument is interpreted as a long int or unsigned long int for integer specifiers (i,d,o, u, xand X), and as a wide character or wide character string for specifiersc ands.

L

The argument is interpreted as a long double (only applies to floating point specifiers:e,E, f, g andG).



additional arguments

Depending on the format string, thefunction may expect a sequence of additional arguments, each containing onevalue to be inserted instead of each%-tag specified in theformat parameter, if any. There should be the same number of these arguments asthe number of%-tags that expect a value.

返回值:成功,返回已写数据的个数;否则,返回一个负数。

 

 

16sscanf

说明:从字符串(缓冲区)里读取数据,格式后存在参数指定的内存区域。

原型:int sscanf (const char * str, const char * format, ...);

参数:str,要读取数据的字符串指针。

format,格式化字符串指针。格式化字符串格式如下:

%[*][width][modifiers]type
                        

*

An optional starting asterisk indicates that the data is to be retrieved from thestr string but ignored, i.e. it is not stored in the corresponding argument.

width

Specifies the maximum number of characters to be read in the current reading operation

modifiers

Specifies a size different from int (in the case of d, i and n), unsigned int (in the case ofo, u andx) or float (in the case of e, f and g) for the data pointed by the corresponding additional argument:
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 andg)
L : long double (for e, f and g)

type

A character specifying the type of data to be read and how it is expected to be read. See next table.


sscanf type specifiers:

type

Qualifying Input

Type of argument

c

Single character: Reads the next character. If a width different from 1 is specified, the function readswidth characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end.

char *

d

Decimal integer: Number optionally preceeded with a + or- sign.

int *

e,E,f,g,G

Floating point: Decimal number containing a decimal point, optionally preceeded by a+ or - sign and optionally folowed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4

float *

o

Octal integer.

int *

s

String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).

char *

u

Unsigned decimal integer.

unsigned int *

x,X

Hexadecimal integer.

int *

 

返回值:成功,返回变量被写的个数;失败,返回EOF

 

 

17vfprintf

说明:将参数经过格式化后写入流中。

原型:int vfprintf (FILE * stream, const char * format, va_list arg );

参数:stream,要写入的流指针。

format,格式字符串指针。

arg,参数列表。

format格式如下:

%[flags][width][.precision][length]specifier

           

specifier

Output

Example

c

Character

a

d or i

Signed decimal integer

392

e

Scientific notation (mantise/exponent) using e character

3.9265e+2

E

Scientific notation (mantise/exponent) using E character

3.9265E+2

f

Decimal floating point

392.65

g

Use the shorter of %e or %f

392.65

G

Use the shorter of %E or %f

392.65

o

Signed octal

610

s

String of characters

sample

u

Unsigned decimal integer

7235

x

Unsigned hexadecimal integer

7fa

X

Unsigned hexadecimal integer (capital letters)

7FA

p

Pointer address

B800:0000

n

Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.

 

%

A % followed by another % character will write % to the stream.


The tag can also contain
 flags, width, .precision and modifiers sub-specifiers,which are optional and follow these specifications:

flags

description

-

Left-justify within the given field width; Right justification is the default (see width sub-specifier).

+

Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.

(space)

If no sign is going to be written, a blank space is inserted before the value.

#

Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
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.

0

Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see widthsub-specifier).

 

width

description

(number)

Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

*

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

 

.precision

description

.number

For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
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.

 

length

description

h

The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).

l

The argument is interpreted as a long int or unsigned long int for integer specifiers (i,d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.

L

The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f,g and G).

 

返回值:成功,返回已写字符的个数;失败,返回负数。

 

 

18vprintf

说明:将参数进过格式化后写入到stdout中。

原型:int vprintf (const char * format, va_list arg );

参数:format,格式化字符串指针。

arg,可变参数列表。

format格式:

%[flags][width][.precision][length]specifier 
                        

specifier

Output

Example

c

Character

a

d or i

Signed decimal integer

392

e

Scientific notation (mantise/exponent) using e character

3.9265e+2

E

Scientific notation (mantise/exponent) using E character

3.9265E+2

f

Decimal floating point

392.65

g

Use the shorter of %e or %f

392.65

G

Use the shorter of %E or %f

392.65

o

Signed octal

610

s

String of characters

sample

u

Unsigned decimal integer

7235

x

Unsigned hexadecimal integer

7fa

X

Unsigned hexadecimal integer (capital letters)

7FA

p

Pointer address

B800:0000

n

Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.

 

%

A % followed by another % character will write % to stdout.


The tag can also contain
 flags, width, .precision and modifiers sub-specifiers,which are optional and follow these specifications:

flags

description

-

Left-justify within the given field width; Right justification is the default (see width sub-specifier).

+

Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.

(space)

If no sign is going to be written, a blank space is inserted before the value.

#

Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
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.

0

Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see widthsub-specifier).

 

width

description

(number)

Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

*

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

 

.precision

description

.number

For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
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.

 

length

description

h

The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).

l

The argument is interpreted as a long int or unsigned long int for integer specifiers (i,d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.

L

The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f,g and G).

返回值:成功,返回已写字符的个数;失败,返回负数。
 

 

19vsprintf

说明:将参数列表格式后写入字符串。

原型:int vsprintf(char * str, const char * format, va_list arg );

参数:str,要写入的字符串指针。

format,格式化字符串指针。

arg,可变参数列表。

format格式如下:

%[flags][width][.precision][length]specifier 

specifier

Output

Example

c

Character

a

d or i

Signed decimal integer

392

e

Scientific notation (mantise/exponent) using e character

3.9265e+2

E

Scientific notation (mantise/exponent) using E character

3.9265E+2

f

Decimal floating point

392.65

g

Use the shorter of %e or %f

392.65

G

Use the shorter of %E or %f

392.65

o

Signed octal

610

s

String of characters

sample

u

Unsigned decimal integer

7235

x

Unsigned hexadecimal integer

7fa

X

Unsigned hexadecimal integer (capital letters)

7FA

p

Pointer address

B800:0000

n

Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.

 

%

A % followed by another % character will write a % sign.


The tag can also contain
 flags, width, .precision and modifiers sub-specifiers,which are optional and follow these specifications:

flags

description

-

Left-justify within the given field width; Right justification is the default (see width sub-specifier).

+

Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.

(space)

If no sign is going to be written, a blank space is inserted before the value.

#

Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively for values different than zero.
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.

0

Left-pads the number with zeroes (0) instead of spaces, where padding is specified (see widthsub-specifier).

 

width

description

(number)

Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.

*

The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

 

.precision

description

.number

For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
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.

 

length

description

h

The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x and X).

l

The argument is interpreted as a long int or unsigned long int for integer specifiers (i,d, o, u, x and X), and as a wide character or wide character string for specifiers c and s.

L

The argument is interpreted as a long double (only applies to floating point specifiers: e, E, f,g and G).

返回值:成功,返回已写字符的个数;失败,返回负数。

 

此文章来自于【http://blog.csdn.net/isgray/article/details/6838341】

你可能感兴趣的:(IO库函数)