scanf,fscanf,sscanf的区别----总结
2016-06-08 22:05
232 查看
fread :以字节位计算长度,按照指定的长度和次数读取数据,遇到结尾或完成指定长度读取后停止.
fscanf :格式化读取,按照指定的格式串进行读取数据,并将数据初始化为指定的格式.所以在文本方式下才能有效的判断数据内容.
fgets :整行读取,遇到回车换行或结尾停止.在文本方式时使用.
其实文本和二进制的差别就在于读取的数据,文本方式会默认为是ASCII码,二进制方式则原样将数据读出.所以文本时候的格式控制如回车,制表符等,
二进制的时候是不能有效的自动判断的。
scanf 从控制台输入
fscanf 从文件输入
sscanf 从指定字符串输入
1、例:使用scanf函数输入数据。
#include<stdio.h>
int main()
{
int a,b,c;
printf("输入 a, b, c\n");
scanf("%d,%d,%d", &a, &b, &c);
printf("a = %d b = %d c = %d\n", a, b, c);
return 0;
}
2、int fscanf(文件指针,格式字符串,输入列表);
功 能: 从一个流中执行格式化输入,fscanf遇到空格和换行时结束,注意空格时也结束。这与fgets有区别,fgets遇到空格不结束。
用 法: int fscanf(FILE *stream, char *format,[argument...]);
如:
FILE *fp;
char a[10];
int b;
double c;
fscanf(fp,"%s%d%lf",a,&b,&c)
返回值:整型,成功读入的参数的个数
3、大家都知道sscanf是一个很好用的函数,利用它可以从字符串中取出整数、浮点数和字符串等等。它的使用方法简单,特别对于整数和浮点数来说。但新手可能并不知道处理字符串时的一些高级用法,这里做个简要说明吧。
1. 常见用法。
char str[512] = {0};
sscanf("123456 ", "%s", str);
printf("str=%s\n", str);
2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。
sscanf("123456 ", "%4s", str);
printf("str=%s\n", str);//str的值为1234
3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。
sscanf("123456 abcdedf", "%[^ ]", str);//注意^后面有一空格
printf("str=%s\n", str);
4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。
sscanf("123456abcdedfBCDEF", "%[1-9a-z]", str);
printf("str=%s\n", str);
5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。
sscanf("123456abcdedfBCDEF", "%[^A-Z]", str);
printf("str=%s\n", str);
源代码一如下:
[html] view
plain copy
print?
#include <stdio.h>
#include <stdlib.h>
char *tokenstring = "12:34:56-7890";
char a1[3], a2[3], a3[3];
int i1, i2;
void main(void)
{
sscanf(tokenstring, "%2s:%2s:%2s-%2d%2d", a1, a2, a3, &i1, &i2);
printf("%s\n%s\n%s\n%d\n%d\n\n", a1, a2, a3, i1, i2);
getch();
}
源代码二如下:
[html] view
plain copy
print?
#include <stdio.h>
#include <stdlib.h>
char *tokenstring = "12:34:56-7890";
char a1[3], a2[3], a3[3],a;
int i1, i2;
void main(void)
{
sscanf(tokenstring, "%2s%1s%2s%1s%2s%1s%2d%2d", a1, &a, a2, &a3, a3, &a, &i1, &i2);
printf("%s\n%s\n%s\n%d\n%d\n\n", a1, a2, a3, i1, i2);
getch();
}
结果同上
源代码三如下:
[html] view
plain copy
print?
#include <stdio.h>
#include <stdlib.h>
char *tokenstring = "12:34:56-7890";
char a1[3], a2[3], a3[3], a4[3], a5[3];
int i1, i2;
void main(void)
{
char a;
sscanf(tokenstring, "%2s%1s%2s%1s%2s%1s%2s%2s", a1, &a, a2, &a3, a3, &a, a4, a5);
i1 =atoi(a4);
i2 =atoi(a5);
printf("%s\n%s\n%s\n%d\n%d\n\n", a1, a2, a3, i1, i2);
getch();
}
结果同上
方法四如下(以实例说明,原理相同):
[html] view
plain copy
print?
/* The following sample illustrates the use of brackets and the
caret (^) with sscanf().
Compile options needed: none
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
char *tokenstring = "first,25.5,second,15";
int result, i;
double fp;
char o[10], f[10], s[10], t[10];
void main()
{
result = sscanf(tokenstring, "%[^','],%[^','],%[^','],%s", o, s, t, f);
fp = atof(s);
i = atoi(f);
printf("%s\n %lf\n %s\n %d\n", o, fp, t, i);
}
fscanf :格式化读取,按照指定的格式串进行读取数据,并将数据初始化为指定的格式.所以在文本方式下才能有效的判断数据内容.
fgets :整行读取,遇到回车换行或结尾停止.在文本方式时使用.
其实文本和二进制的差别就在于读取的数据,文本方式会默认为是ASCII码,二进制方式则原样将数据读出.所以文本时候的格式控制如回车,制表符等,
二进制的时候是不能有效的自动判断的。
scanf 从控制台输入
fscanf 从文件输入
sscanf 从指定字符串输入
1、例:使用scanf函数输入数据。
#include<stdio.h>
int main()
{
int a,b,c;
printf("输入 a, b, c\n");
scanf("%d,%d,%d", &a, &b, &c);
printf("a = %d b = %d c = %d\n", a, b, c);
return 0;
}
2、int fscanf(文件指针,格式字符串,输入列表);
功 能: 从一个流中执行格式化输入,fscanf遇到空格和换行时结束,注意空格时也结束。这与fgets有区别,fgets遇到空格不结束。
用 法: int fscanf(FILE *stream, char *format,[argument...]);
如:
FILE *fp;
char a[10];
int b;
double c;
fscanf(fp,"%s%d%lf",a,&b,&c)
返回值:整型,成功读入的参数的个数
3、大家都知道sscanf是一个很好用的函数,利用它可以从字符串中取出整数、浮点数和字符串等等。它的使用方法简单,特别对于整数和浮点数来说。但新手可能并不知道处理字符串时的一些高级用法,这里做个简要说明吧。
1. 常见用法。
char str[512] = {0};
sscanf("123456 ", "%s", str);
printf("str=%s\n", str);
2. 取指定长度的字符串。如在下例中,取最大长度为4字节的字符串。
sscanf("123456 ", "%4s", str);
printf("str=%s\n", str);//str的值为1234
3. 取到指定字符为止的字符串。如在下例中,取遇到空格为止字符串。
sscanf("123456 abcdedf", "%[^ ]", str);//注意^后面有一空格
printf("str=%s\n", str);
4. 取仅包含指定字符集的字符串。如在下例中,取仅包含1到9和小写字母的字符串。
sscanf("123456abcdedfBCDEF", "%[1-9a-z]", str);
printf("str=%s\n", str);
5. 取到指定字符集为止的字符串。如在下例中,取遇到大写字母为止的字符串。
sscanf("123456abcdedfBCDEF", "%[^A-Z]", str);
printf("str=%s\n", str);
源代码一如下:
[html] view
plain copy
print?
#include <stdio.h>
#include <stdlib.h>
char *tokenstring = "12:34:56-7890";
char a1[3], a2[3], a3[3];
int i1, i2;
void main(void)
{
sscanf(tokenstring, "%2s:%2s:%2s-%2d%2d", a1, a2, a3, &i1, &i2);
printf("%s\n%s\n%s\n%d\n%d\n\n", a1, a2, a3, i1, i2);
getch();
}
源代码二如下:
[html] view
plain copy
print?
#include <stdio.h>
#include <stdlib.h>
char *tokenstring = "12:34:56-7890";
char a1[3], a2[3], a3[3],a;
int i1, i2;
void main(void)
{
sscanf(tokenstring, "%2s%1s%2s%1s%2s%1s%2d%2d", a1, &a, a2, &a3, a3, &a, &i1, &i2);
printf("%s\n%s\n%s\n%d\n%d\n\n", a1, a2, a3, i1, i2);
getch();
}
结果同上
源代码三如下:
[html] view
plain copy
print?
#include <stdio.h>
#include <stdlib.h>
char *tokenstring = "12:34:56-7890";
char a1[3], a2[3], a3[3], a4[3], a5[3];
int i1, i2;
void main(void)
{
char a;
sscanf(tokenstring, "%2s%1s%2s%1s%2s%1s%2s%2s", a1, &a, a2, &a3, a3, &a, a4, a5);
i1 =atoi(a4);
i2 =atoi(a5);
printf("%s\n%s\n%s\n%d\n%d\n\n", a1, a2, a3, i1, i2);
getch();
}
结果同上
方法四如下(以实例说明,原理相同):
[html] view
plain copy
print?
/* The following sample illustrates the use of brackets and the
caret (^) with sscanf().
Compile options needed: none
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
char *tokenstring = "first,25.5,second,15";
int result, i;
double fp;
char o[10], f[10], s[10], t[10];
void main()
{
result = sscanf(tokenstring, "%[^','],%[^','],%[^','],%s", o, s, t, f);
fp = atof(s);
i = atoi(f);
printf("%s\n %lf\n %s\n %d\n", o, fp, t, i);
}
相关文章推荐
- 内建变量与线程索引
- 299.[leetcode]Bulls and Cows
- 使用Brackets编写Sass代码
- 云原生应用程序架构的五大特性(上)- 12要素应用
- 第14章 抽象类和接口
- 0608 (用户故事)任务-具体工作流程安排
- 把一个内容为16进制 的文本文件,转换成10进制的文本文件
- 机器学习、自然语言处理、大数据和keras学习资料
- java图形和文本学习
- 张小军:象征资本的再生产——从阳村宗族论民国基层社会
- EditText设置IME动作问题
- 折半插入排序
- Ubuntu 14.04+Opencv-2.4.9+Qt配置
- 2016-6-8
- 梦断代码阅读笔记01
- Appium+Robotframework实现手机应用的自动化测试-1:Appium简介
- 用Redis存储Tomcat集群实现Session共享
- 第15周阅读程序(1)
- 【HTML笔记三】认识标签二
- .典型用户 - 场景