sscanf-30

问题描述:单片机sscanf函数问题 本篇文章给大家谈谈一个有趣的事情,以及一个有趣的事情,希望对各位有所帮助,不要忘了收藏本站喔。

c语言编程,关于sscanf通过中文逗号截取字符串的问题

sscanf-30的相关图片

头文件 #include

定义函数 int sscanf (const char *str,const char * format,........);。

函数说明 。

sscanf()会将参数str的字符串根据参数format字符串来转换并格式化数据。格式转换形式请参考scanf()。转换后的结果存于对应的参数内。

返回值 成功则返回参数数目,失败则返回-1,错误原因存于errno中。 返回0表示失败 否则,表示正确格式化数据的个数 例如:sscanf(str,"%d%d%s", &i,&i2, &s); 如果三个变成都读入成功会返回3。 如果只读入了第一个整数到i则会返回1。证明无法从str读入第二个整数。

范例 #include 。

main() 。

{

int i; 。

unsigned int j; 。

char input[ ]=”10 0x1b aaaaaaaa bbbbbbbb”; 。

char s[5]; 。

sscanf(input,”%d %x %5[a-z] %*s %f”,&i,&j,s,s); 。

printf(“%d %d %s ”,i,j,s); 。

}

执行 10 27 aaaaa。

sscanf(stringBuf.c_str(), "%20[^#]#%20[^ ]",......)语句中""中的内容含义为:

“%[ 。

]”符号用于声明字符串,它比“%s”更具体,可以用于设置读取的样式。例如“%[a-z]”只读取小写字母,读到其它字符就结束。注意,方括号中如果有“^”,代表一直读到某字符为止。例如:

“%[^#]”:读取字符串,一直到出现“#”号为止。

“%20[^#]”:读取20个字节的字符串,出现“#”号时结束。

所以,“%20[^#]#%20[^ ]”的意义就是,

读取两个20字节大小的字符串,第一个字符串可以用#结束,第二个字符串可以用回车符结束。

关于sscanf的问题的相关图片

关于sscanf的问题

C语言sscanf函数的总结

在处理字符串的程序当中,经常会分析字符串,从一大长串的字符串截取我们需要的数据,这如果通过自己手写函数来分析,虽然可以,但当你知道sscanf的功能,那就自己写函数分析就显得多此一举。

这些函数的使用都很简单,总结一下,等下次使用一目了然。俗话说:好记性不如烂笔头,记录下来就是效率。

以下源代码是本人测试的源文件,附带讲解注释。

1./***************************************************** 。

2.** Name : sscanf.c 。

3.** Author : gzshun 。

4.** Version : 1.0 。

5.** Date : 2011-12 。

6.** Description : sscanf function 。

7.******************************************************/ 。

8.#include <stdio.h> 。

9.#include <stdlib.h> 。

10.#include <string.h> 。

11.

12.static void sscanf_test(void); 。

13.

14.static void sscanf_test(void) 。

15.{

16. int ret; 。

17. char *string; 。

18. int digit; 。

19. char buf1[255]; 。

20. char buf2[255]; 。

21. char buf3[255]; 。

22. char buf4[255]; 。

23.

24. /*1.最简单的用法*/ 。

25. string = "china beijing 123"; 。

26. ret = sscanf(string, "%s %s %d", buf1, buf2, &digit); 。

27. printf("1.string=%s\n", string); 。

28. printf("1.ret=%d, buf1=%s, buf2=%s, digit=%d\n\n", ret, buf1, buf2, digit); 。

29. /*

30. **执行结果: 。

31. **1.ret=2, buf1=china, buf2=beijing, digit=123 。

32. **可以看出,sscanf的返回值是读取的参数个数 。

33. */

34.

35. /*2.取指定长度的字符串*/ 。

36. string = "123456789"; 。

37. sscanf(string, "%5s", buf1); 。

38. printf("2.string=%s\n", string); 。

39. printf("2.buf1=%s\n\n", buf1); 。

40. /*

41. **执行结果: 。

42. **2.buf1=12345 。

43. */

44.

45. /*3.取到指定字符为止的字符串*/ 。

46. string = "123/456"; 。

47. sscanf(string, "%[^/]", buf1); 。

48. printf("3.string=%s\n", string); 。

49. printf("3.buf1=%s\n\n", buf1); 。

50. /*

51. **执行结果: 。

52. **3.buf1=123 。

53. */

54.

55. /*4.取到指定字符集为止的字符串*/ 。

56. string = "123abcABC"; 。

57. sscanf(string, "%[^A-Z]", buf1); 。

58. printf("4.string=%s\n", string); 。

59. printf("4.buf1=%s\n\n", buf1); 。

60. /*

61. **执行结果: 。

62. **4.buf1=123abc 。

63. */

64.

65. /*5.取仅包含指定字符集的字符串*/ 。

66. string = "0123abcABC"; 。

67. sscanf(string, "%[0-9]%[a-z]%[A-Z]", buf1, buf2, buf3); 。

68. printf("5.string=%s\n", string); 。

69. printf("5.buf1=%s, buf2=%s, buf3=%s\n\n", buf1, buf2, buf3); 。

70. /*

71. **执行结果: 。

72. **5.buf1=123 。

73. */

74.

75. /*6.获取指定字符中间的字符串*/ 。

76. string = "ios<Android>wp7"; 。

77. sscanf(string, "%*[^<]<%[^>]", buf1); 。

78. printf("6.string=%s\n", string); 。

79. printf("6.buf1=%s\n\n", buf1); 。

80. /*

81. **执行结果: 。

82. **6.buf1=android 。

83. */

84.

85. /*7.指定要跳过的字符串*/ 。

86. string = "iosVSandroid"; 。

87. sscanf(string, "%[a-z]VS%[a-z]", buf1, buf2); 。

88. printf("7.string=%s\n", string); 。

89. printf("7.buf1=%s, buf2=%s\n\n", buf1, buf2); 。

90. /*

91. **执行结果: 。

92. **7.buf1=ios, buf2=android 。

93. */

94.

95. /*8.分割以某字符隔开的字符串*/ 。

96. string = "android-iphone-wp7"; 。

97. /*

98. **字符串取道'-'为止,后面还需要跟着分隔符'-', 。

99. **起到过滤作用,有点类似于第7点 。

100. */

101. sscanf(string, "%[^-]-%[^-]-%[^-]", buf1, buf2, buf3); 。

102. printf("8.string=%s\n", string); 。

103. printf("8.buf1=%s, buf2=%s, buf3=%s\n\n", buf1, buf2, buf3); 。

104. /*

105. **执行结果: 。

106. **8.buf1=android, buf2=iphone, buf3=wp7 。

107. */

108.

109. /*9.提取邮箱地址*/ 。

110. string = "Email:beijing@sina.com.cn"; 。

111. sscanf(string, "%[^:]:%[^@]@%[^.].%s", buf1, buf2, buf3, buf4); 。

112. printf("9.string=%s\n", string); 。

113. printf("9.buf1=%s, buf2=%s, buf3=%s, buf4=%s\n\n", buf1, buf2, buf3, buf4); 。

114. /*

115. **执行结果: 。

116. **9.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn 。

117. */

118.}

119.

120.int main(int argc, char **argv) 。

121.{

122. sscanf_test(); 。

123.

124. return 0; 。

125.}

126.

127./*

128.**测试程序

129.**环境:

130.**Linux Ubuntu 2.6.32-24-generic-pae #39-Ubuntu SMP Wed Jul 28 07:39:26 UTC 2010 i686 GNU/Linux 。

131.**gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) 。

132.**

133.gzshun@ubuntu:~/c/sscanf$ gcc sscanf.c -o sscanf 。

134.gzshun@ubuntu:~/c/sscanf$ ./sscanf 。

135.1.string=china beijing 123 。

136.1.ret=3, buf1=china, buf2=beijing, digit=123 。

137.

138.2.string=123456789 。

139.2.buf1=12345 。

140.

141.3.string=123/456 。

142.3.buf1=123 。

143.

144.4.string=123abcABC 。

145.4.buf1=123abc 。

146.

147.5.string=0123abcABC 。

148.5.buf1=0123, buf2=abc, buf3=ABC 。

149.

150.6.string=ios<android>wp7 。

151.6.buf1=android 。

152.

153.7.string=iosVSandroid 。

154.7.buf1=ios, buf2=android 。

155.

156.8.string=android-iphone-wp7 。

157.8.buf1=android, buf2=iphone, buf3=wp7 。

158.

159.9.string=Email:beijing@sina.com.cn 。

160.9.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn 。

161.*/

本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2011-12/49711.htm。

sscanf()如何实现?的相关图片

sscanf()如何实现?

请检查你的s[i]是什么内容,还有第二个sscanf少了一个d,是%d!

================。

"%fl" 你写反了,是lf( long float)。

sscanf(...,"%lf\t",d);可以读。

具体你看下面的msdn吧

Read formatted data from a string. These functions are deprecated because more secure versions are available; see sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l.。

int sscanf(

const char *buffer,。

const char *format [,。

argument ] ... 。

);

int _sscanf_l(

const char *buffer,。

const char *format,。

locale_t locale [,。

argument ] ... 。

);

int swscanf(

const wchar_t *buffer,。

const wchar_t *format [,。

argument ] ... 。

);

int _swscanf_l(。

const wchar_t *buffer,。

const wchar_t *format,。

locale_t locale [,。

argument ] ... 。

);

Parameters

buffer

Stored data

format

Format-control string. For more information, see Format Specifications.。

argument

Optional arguments。

locale

The locale to use。

Return Value

Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.。

If buffer or format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions return -1 and set errno to EINVAL.。

For information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.。

Remarks

The sscanf function reads data from buffer into the location given by each argument. Every argument must be a pointer to a variable with a type that corresponds to a type specifier in format. The format argument controls the interpretation of the input fields and has the same form and function as the format argument for the scanf function. If copying takes place between strings that overlap, the behavior is undefined.。

Security Note 。

When reading a string with sscanf, always specify a width for the %s format (for example, "%32s" instead of "%s"); otherwise, improperly formatted input can easily cause a buffer overrun.。

swscanf is a wide-character version of sscanf; the arguments to swscanf are wide-character strings. sscanf does not handle multibyte hexadecimal characters. swscanf does not handle Unicode full-width hexadecimal or "compatibility zone" characters. Otherwise, swscanf and sscanf behave identically.。

The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.。

Generic-Text Routine Mappings。

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined 。

_stscanf

sscanf

sscanf

swscanf

_stscanf_l

_sscanf_l

_sscanf_l

_swscanf_l

Requirements

Routine Required header Compatibility 。

sscanf, _sscanf_l。

<stdio.h>。

ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003。

swscanf, _swscanf_l。

<stdio.h> or <wchar.h>。

ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003。

For additional compatibility information, see Compatibility in the Introduction.。

Example

Copy Code

// crt_sscanf.c。

// compile with: /W1。

// This program uses sscanf to read data items。

// from a string named tokenstring, then displays them.。

#include <stdio.h>。

int main( void )。

char tokenstring[] = "15 12 14...";。

char s[81];。

char c;

int i;

float fp;

// Input various data from tokenstring:。

// max 80 character string:。

sscanf( tokenstring, "%80s", s ); // C4996。

sscanf( tokenstring, "%c", &c ); // C4996。

sscanf( tokenstring, "%d", &i ); // C4996。

sscanf( tokenstring, "%f", &fp ); // C4996。

// Note: sscanf is deprecated; consider using sscanf_s instead。

// Output the data read。

printf( "String = %s\n", s );。

printf( "Character = %c\n", c );。

printf( "Integer: = %d\n", i );。

printf( "Real: = %f\n", fp );。

Output

String = 15

Character = 1

Integer: = 15

Real: = 15.000000。

.NET Framework Equivalent。

See Parse methods, such as System::Double::Parse.。

See Also

Reference

Stream I/O

fscanf, _fscanf_l, fwscanf, _fwscanf_l。

scanf, _scanf_l, wscanf, _wscanf_l。

sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l。

_snprintf, _snprintf_l, _snwprintf, _snwprintf_l。

地址:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_vccrt/html/c2dcf0d2-9798-499f-a4a8-06f7e2b9a80c.htm。

C语言中 字符串如何转为整型数值的相关图片

C语言中 字符串如何转为整型数值

sscanf与scanf类似,都是用于输入的,只是后者以键盘(stdin)为输入源,前者以固定字符串为输入源。

举个例子给你看:

char* pQueryStr=getenv("QUERY_STRING");。

char pName[256]; 。

sscanf(pQueryStr,"name=%s",pName); 。

printf("Hello %s!\n",pName);。

最后显示出来的就是:Hello tom!。

c语言sscanf语句如何解释

在C语言中将字符串转化成整型有两种方法。

1 用atoi函数。

atoi的功能就是将字符串转为整型并返回。其声明为。

int atoi(char *str);。

比如atoi("1234");会返回整型1234。

要调用atoi,需要引用头文件stdio.h。

2 用sscanf。

sscanf与标准格式化输入函数scanf类似,不过源并非是标准输入,而是字符串。

用sscanf可以处理更复杂的字符串。

比如字符串char * str = "a=1, b=2";。

定义int a,b;后

可以用

sscanf(str,"a=%d, b=%d",&a,&b);。

来将a,b值提取,计算后,a=1, b=2。

要使用sscanf同样需要引用头文件stdio.h。

原文地址:http://qianchusai.com/sscanf-30.html

世界十大蓄电池品牌,世界十大蓄电池品牌有哪些

世界十大蓄电池品牌,世界十大蓄电池品牌有哪些

BOxcovers-50

BOxcovers-50

lw/老太公园偷醒图片,老太太逛公园下一句

lw/老太公园偷醒图片,老太太逛公园下一句

愤疑-50

愤疑-50

divert-40

divert-40

crash-90

crash-90

completion,completion怎么读英语

completion,completion怎么读英语

天生不同pdf百度云,天生不同pdf百度云资源

天生不同pdf百度云,天生不同pdf百度云资源

s-340-160

s-340-160

描写人物认真思考时神态的句子,描写人物认真思考时神态的句子摘抄

描写人物认真思考时神态的句子,描写人物认真思考时神态的句子摘抄

三国志战略版八级地速通攻略 - 最快通关阵容与技巧 三国志战略版乐府系统专题 - 完整攻略与玩法指南 三国志战略版拔城技巧 - 实用攻城攻略大全 三国志战略版红蜀智阵容攻略 - 最强蜀国智力队搭配指南 三国志战略版调兵和驻守攻略 - 军事部署完全指南 三国志战略版八级地攻打攻略 - 阵容搭配与打法详解 三国志战略版治疗率计算器 - 精准计算武将治疗能力 三国志战略版兑换码大全 - 最新有效兑换码汇总 三国志战略版乐府有必要吗?深度解析乐府价值与投资建议 三国志战略版秘策效果大全 - 游戏攻略指南 三国志战略版势力介绍 - 魏蜀吴群雄详解攻略 三国志战略版自愈攻略 - 完整攻略指南 三国志战略版测试服 - 最新版本抢先体验 | 官方测试服务器 三国志战略版满级名声攻略 - 最全名声提升指南 三国志战略版吕布阵容推荐 - 最强吕布阵容搭配攻略 三国志战略版高级建筑分配攻略 - 最优建筑布局与资源分配指南 三国志战略版如何提高势力值 - 完整攻略指南 三国志战略版体验服申请 - 提前体验最新游戏内容 三国志战略版文丑武将攻略 - 技能搭配与使用指南 三国志战略版七级土地攻略 - 占领条件、资源产出与防守策略 三国志战略版吕布张辽组合攻略 - 最强输出搭配详解 三国志战略版8级地要多少兵力 - 详细攻略与计算器 三国志战略版名声系统详解 - 声望提升攻略大全 三国志战略版申请资格 - 官方申请指南 三国志战略版大盟进司隶 - 游戏攻略与联盟指南 三国志战略版拔城指令 - 完整攻略指南 三国志战略版先锋测试服申请 - 率先体验全新内容 三国志战略版如何获得名声 - 完整攻略指南 三国志战略版同类冲突 - 游戏攻略与策略分析 三国志战略版15000势力值任务攻略 - 快速达成指南