C语言fscanf的用法是什么

Python014

C语言fscanf的用法是什么,第1张

用 法: int fscanf(FILE *stream, char *format,[argument...])。

1、fscanf()的format参数允许的格式为:“%[*][width][modifiers]type”。[]中的内容根据需要供选,可缺省,%type必须要有,不可省。

2、“format”是C字符串,由“空格”、“非空格”及“转换符”组成。具体格式为%[*][width][modifiers]type,与“format”中“转换符”对应变量地址的列表,两地址间用逗号隔开。

fscanf()对空格的处理示例代码如下:

re  = fscanf(pF, "%d%d%d", &oLine.p, &oLine.l, &oLine.x)  

if( 3 == re){  

        //Print on stdout  

        printf("%d\t%d\t%d\n", oLine.p, oLine.l, oLine.x)  

}

fprintf(fp,"%s,%c,%d,%f",str,a,

c,

b)

这个输出格式表明

你的文件1.txt

里的数据

是用

逗号

隔。

if((fp

=

fopen("1.txt","r"))==NULL)

你要打开

这个

逗号为

分隔符

的文件。

fscanf(fp,"%s,%c,%d,%f",

str,

&a,

&c,

&b)

漏写

str,

给你补上,但

这仍不能解决

%s,

的逗号分隔问题。

必须

用下面格式读取逗号分隔的数据:

fscanf(fp,"%[^,],%c,%d,%f",

str,

&a,

&c,

&b)

======================================

假如文件里的数据

空白

分隔,不用

逗号,日子就好过得多:

fprintf(fp,"%s

%c

%d

%f",str,a,

c,

b)

fscanf(fp,"%s

%c

%d

%f",

str,

&a,

&c,

&b)