scanf,fscanf,sscanf的区别----整理
转自原文 scanf,fscanf,sscanf的区别----整理

scanf 从控制台输入
fscanf 从文件输入
sscanf 从指定字符串输入
1、例:使用scanf函数输入数据。
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);
源代码一如下:
#include <stdio.h>
#include <stdlib.h> char *tokenstring = "12:34:56-7890";
char a1[], a2[], a3[];
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();
}
源代码二如下:
#include <stdio.h>
#include <stdlib.h> char *tokenstring = "12:34:56-7890";
char a1[], a2[], a3[],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();
}
结果同上
源代码三如下:
#include <stdio.h>
#include <stdlib.h> char *tokenstring = "12:34:56-7890";
char a1[], a2[], a3[], a4[], a5[];
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();
}
结果同上
方法四如下(以实例说明,原理相同):
/* 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[], f[], s[], t[]; 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);
}
scanf,fscanf,sscanf的区别----整理的更多相关文章
- scanf,fscanf,sscanf的区别
scanf是从文件中读 sscanf是从字符串中读 scanf是从键盘输入中读 fread :以字节位计算长度,按照指定的长度和次数读取数据,遇到结尾或完成指定长度读取后停止.fscanf :格式 ...
- IHttpModule与IHttpHandler的区别整理
IHttpModule与IHttpHandler的区别整理1.先后次序.先IHttpModule,后IHttpHandler. 注:Module要看你响应了哪个事件,一些事件是在Handler之前运行 ...
- C语言中scanf/fscanf 的%[]和%n说明符的使用方法
标准输入输出函数%[]和%n说明符的使用方法 scanf fscanf,均从第一个非空格的可显示字符开始读起! 标准输入输出函数scanf具有相对较多的转换说明符,它常常作为入 ...
- scanf()与gets()的区别
scanf()与gets()的区别 1.scanf()可以同时接受多个字符串,而gets()一次只能接受一个字符串. #include<stdio.h>int main(){ char s ...
- 关于scanf与gets的区别
以下内容主要来源: scanf与gets读取字符串 scanf与gets函数读取字符串的区别 前两天有个同学问我scanf与gets的区别说了半天也没说出来个所以然,就搜了一下,scanf()和get ...
- scanf与scanf_s的区别
scanf()函数是标准C中提供的标准输入函数,用以用户输入数据 scanf_s()函数是Microsoft公司VS开发工具提供的一个功能相同的安全标准输入函数,从vc++2005开始,VS系统提供了 ...
- scanf和scanfs的区别
scanf()函数是标准C中提供的标准输入函数,用以用户输入数据 scanf_s()函数是Microsoft公司VS开发工具提供的一个功能相同的安全标准输入函数,从vc++2005开始,VS系统提供了 ...
- C中scanf/gets/fgets的区别
功能:同样是获取字符串. 区别: scanf 遇到空格/回车/Tab键认为输入结束, 但是空格/回车/Tab键仍会留在输入的缓冲区中.常见的是使用getchar(),处理scanf的后事:如果想要清除 ...
- scanf(),fscanf的详解
我们这里只讨论fscanf(或者scanf)的格式,因为这些细节在其他贴里并没有涉及,阅读此文,你可以少走一些弯路.只讲结果,深层原因并不分析. FILE *pFile:float x1; char ...
随机推荐
- ThinkPHP项目怎么运行?
1.下载ThinkPHP项目 2.安装核心框架framework 3.配置集成开发环境:wamp或者xampp或者phpStudy
- 有趣的this以及apply,call,bind方法
看this指向谁,要看执行时而非定义时(箭头函数除外).函数没有绑定在对象上调用,非'strict'模式下,this指向window,否则为undefined 改变this指向的方法 1. apply ...
- (13)zabbix External checks 外部命令检测
1. 概述 zabbix server运行脚本或者二进制文件来执行外部检测,外部检测不需要在被监控端运行任何agentd item key语法如下: ARGUMENT DEFINITION scri ...
- Linux中断底半部机制
参考: Linux下半部处理之软中断 linux中断底半部机制 <深入理解Linux内核>软中断/tasklet/工作队列 软中断和tasklet介绍 详解操作系统中断 Linux内核:中 ...
- 【HDU 2126】Buy the souvenirs(01背包)
When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souve ...
- ORACLE 查询所有用户调度作业
--查询所有用户调度作业:select * from ALL_SCHEDULER_JOBS; --查询当前用户调度作业:select * from USER_SCHEDULER_JOBS; --查询当 ...
- java 协程框架quasar gradle配置
https://github.com/puniverse/quasar-gradle-template/blob/master/gradle/agent.gradle 1.将其中的"-jav ...
- Java-获取一个类的父类
如何使用代码获取一个类的父类 package com.tj; public class MyClass implements Cloneable { public static void main(S ...
- 分离焦虑OR责任焦虑
这里是用小孩上幼儿园的事说分离焦虑,转念到成人身上就是责任焦虑. 这周小孩开始上幼儿园了,他很害怕家长离开,我能做的也不多,只是很肯定的告诉他,爸爸就在停车场,下学就来接你,然后从各个 ...
- Python内置函数7
Python内置函数7 1.propertypython内置的一个装饰器可参考https://blog.csdn.net/u013205877/article/details/77804137 2.q ...