sscanf如何取出"0xFFBC"字符串?

jiahehao 2013-05-23 07:41:14
  一个字符串“5,10,str,0xFFBC”.
我用sscanf来截取:

int a,b;
char c[16];
char d[16];

sscanf(buf, "%d, %d, %s, %s ", &a, &b, c, d);
   printf("the date is %d, %d, %s, %s.\n",a,b,c,d)

sscanf(buf, "%d, %d, %s, %x ", &a, &b, c, d);
  printf("the date is %d, %d, %s, %x.\n",a,b,c,d)

   但上面的两种方法都截取不出 “0xFFBC”。
   请问应该如何写?
...全文
241 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiahehao 2013-05-24
  • 打赏
  • 举报
回复
唉,楼上各位兄台武功远胜于我。。。。。。 问题解决,多谢各位指教。
qq120848369 2013-05-23
  • 打赏
  • 举报
回复
别用%s来调用scanf/sscanf,内存溢出风险。
lunat 2013-05-23
  • 打赏
  • 举报
回复


#include <stdio.h>
#include <stdlib.h>

int main ()
{
    int a, b, d;
    char c[16];
    
	char buf[] = "5, 10, str, 0xFFBC";
	
	sscanf(buf, "%d, %d, %[^,], 0x%X", &a, &b, c, &d);
	printf("the date is %d, %d, %s, 0x%X.\n", a, b, c, d);
	
	sscanf(buf, "%*d, %*d, %*[^,], %s", c);
	
	printf("c=%s\n", c);
	
	return 0;
}
绯红女王 2013-05-23
  • 打赏
  • 举报
回复
(补充楼上,按错了) 指针指向目标字符串的首地址
绯红女王 2013-05-23
  • 打赏
  • 举报
回复
顶楼上···strstr搜索目标字符串,并返回一个指针,指针指向
Carl_CCC 2013-05-23
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int  main(void )
{
	int a,b;
	char c[16];
	char d[16];
	char * p;
	char buf[128] = "5, 10, str,0xFFBC";

	p=strstr(buf,"str,");
	if(p)
	{
		p=p+4;
		strcpy(d, p);
		printf("BUF:%s\n",d);
	}


	return 0;
} 
附个代码吧。
Carl_CCC 2013-05-23
  • 打赏
  • 举报
回复
字符串的处理使用sscanf会很麻烦,比如,也是字符串的也部分,会和前一个字符串连接起来,你这就是str,0xFFBC当成一个字符串传给C了,所以d就不知道什么东西了。 字符串的处理一般用strstr来搜索定位

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧