sscanf获取字符串中的内容

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void)
{
	char buf[] = "1234 5678";
	int a;
	sscanf(buf, "%*d %d", &a);
	printf("a = %d\n", a);

	char s1[100];
	sscanf("mike abc", "%*s %s", s1);
	printf("s1 = %s\n", s1);

	char buf2[] = "abc123";
	char s2[100];
	sscanf(buf2, "%2s", s2);
	printf("s2 = %s\n", s2);

	char buf3[] = "azbcsdgfsd1234";
	char s3[100];
	sscanf(buf3, "%*[a-z]%[0-9]", s3);
	printf("s3 = %s\n", s3);

	//最后一种
	char buf4[] = "abc,hello,mike";
	char str1[100], str2[100], str3[100];
	//sscanf(buf4, "%s,%s,%s", str1, str2, str3); //不可以
	//找字符,指定遇到逗号结束
	sscanf(buf4, "%[^,]", str1);
	printf("str1 = %s\n", str1);

	sscanf(buf4, "%*[^,],%[^,]", str2);
	printf("str2 = %s\n", str2);

	sscanf(buf4, "%*[^m]%[^i]", str3);
	printf("str3 = %s\n", str3);


	printf("\n");
	system("pause");
	return 0;
}
/*
a = 5678
s1 = abc
s2 = ab
s3 = 1234
str1 = abc
str2 = hello
str3 = m

请按任意键继续. . .


*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值