sscanf-70

问题描述:关于sscanf()函数 这篇文章主要介绍了一个有趣的事情,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。

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

sscanf-70的相关图片

“\”

这个只是说代码要分行书写时,在其后加"\"就代表这一行的内容与下一行的内容是连续的。

比如

int a\

,b;

其实就是一行int a,b;

代码编译是逐句的,早期编译器会把一行看成一句,所以为了”提醒“编译器,这一行与下一行是连续的。我们就在行末加"\";。

而现在的编译器一般来说是不需要的。

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函数问题

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

举个例子给你看:

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

char pName[256]; 。

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

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

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

关于sscanf的问题的相关图片

关于sscanf的问题

头文件 #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字节大小的字符串,第一个字符串可以用#结束,第二个字符串可以用回车符结束。

C语言中的sprint函数问题

请检查你的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。

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

黑苹果解决独显HDMI黑屏问题-50,黑苹果hdmi输出黑屏但有声音

黑苹果解决独显HDMI黑屏问题-50,黑苹果hdmi输出黑屏但有声音

7811-100

7811-100

multiagency-90

multiagency-90

藏之中电影,藏身之处电视剧在线观看

藏之中电影,藏身之处电视剧在线观看

majorette-80

majorette-80

flute,flute什么乐器

flute,flute什么乐器

employee-40

employee-40

一切因缘生-50,一切因缘生,一切因缘灭是什么意思

一切因缘生-50,一切因缘生,一切因缘灭是什么意思

prunes,prune是什么牌子包

prunes,prune是什么牌子包

playboy-40

playboy-40

三国志战略版吕布张辽组合攻略 - 最强输出搭配详解 三国志战略版土地守军攻略大全 - 完整攻略指南 三国志战略版暴戾无仁攻略 - 技能解析、阵容搭配、使用技巧 三国志战略版128级出核心攻略 - 完整指南与技巧分享 三国志战略版兵锋武将专题 - 兵锋技能详解与武将搭配 三国志战略版先锋测试服福利 - 豪华礼包等你来领 三国志14战法搭配攻略 - 最强战法组合推荐 三国志14战法效果大全 - 最全战法数据攻略 三国志战略版体验服 - 最新版本抢先体验 三国志战略版策书怎么获得 - 策书获取方法大全 三国志战略版造币厂数量上限详解 - 游戏攻略指南 三国志战略版盾墙剧本攻略大全 - 最新玩法技巧详解 三国志战略版夏侯惇技能效果详解 - 全方位攻略指南 三国志战略版虎杖多大 - 虎杖属性技能详解 三国志战略版八级地练级攻略 - 最强武将搭配与战术指南 三国志战略版预备兵3万攻略 - 预备兵获取与使用指南 三国志战略版拔城指令 - 完整攻略指南 三国志战略版军屯值得买吗?军屯系统全面分析 - 游戏攻略 三国志战略版天下骑T0阵容攻略 - 完整搭配指南 三国志战略版满级造币厂 - 升级攻略与属性详解 三国志战略版战法冲突效果详解 - 游戏攻略指南 三国志战略版名声上限是多少 - 完整攻略指南 三国志战略版控制类武将完全攻略 - 技能搭配与阵容推荐 三国志战略版夏侯渊兵书推荐攻略 - 最佳搭配与使用技巧 三国志战略版怎么加灼烧 - 灼烧效果全攻略 三国志战略版夏侯渊T0阵容推荐 - 最强阵容搭配攻略 三国志战略版怎么快速提高势力值 - 实用攻略指南 三国志战略版乐府系统详解 - 攻略指南 三国志战略版经武战法全攻略 - 经武战法详解与搭配推荐 三国志战略版坐守孤城攻略大全 - 守城策略与技巧