fscanf()函数
-
头文件:stdio.h
-
函数定义:int fscanf(FILE * stream, const char *format, [argument…]);
- stream:文件指针
- format:转换格式,比如%d,%f等
- argument:参数地址表,与scanf类似
-
函数说明:从stream 的文件流中顺序读取字符, 再根据format来转换并格式化数据。
- 转换后的结构存于对应的参数内
-
返回值:
- 成功则返回参数数目
- 失败则返回-1, 错误原因存于errno中
使用示例
-
新建并编辑参数文件
-
touch test.txt
-
gedit test.txt
-
-
测试程序
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp = fopen("/home/xs/test.txt","r");
int i,j;
for (i = 0; i < 7; i++)
{
fscanf(fp,"%d",&j);
printf("%d\n",j);
}
fclose(fp);
return 0;
}
- 程序输出
- 修改参数文件为
- 程序添加 “,”-format字段
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp = fopen("/home/xs/test.txt","r");
int i,j;
for (i = 0; i < 7; i++)
{
fscanf(fp,"%d,",&j);
printf("%d\n",j);
}
fclose(fp);
return 0;
}
- 输出相同结果
http://c.biancheng.net/cpp/html/292.html
http://blog.csdn.net/liangxanhai/article/details/8026496
2017.06.21