sscanf-的用法

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

void main()
{
	//如果是以逗号做分隔的就应该把 , 换成空格 
	char str[] = "51934,雷小姐,女,21,166, 办公室职员,100元/天,北京电影学院形象气质佳, 123456789, 123@qqc.om, 987654321";
	for (int i = 0; i < strlen(str); i++)
	{
		if (str[i] == ',')
		{
			str[i] = ' ';
		}
	}
	printf("%s\n", str);
	//char str[] = "51934	雷小姐	女	21 	166 办公室职员	100元/天	北京电影学院形象气质佳 123456789 123@qqc.om 987654321";
	
	int id;
	char name[10];
	char sex[10];
	int age;
	int tall;
	int sf[10];
	char price[100];     
	char introduce[500];
	long long phone;
	char email[100];
	long long QQ;
	//注意使用 scanf() 与 sscanf()的区别,sscanf()是从buffer缓冲区中读取数据
	//scanf(str, "%d%s%s%d%d%s%s%s%lld%s%lld", &id, name, sex, &age, &tall, sf, price, introduce, &phone, email, &QQ);
	/* 是输入从buffer(缓冲区)中读取. */
	sscanf(str, "%d%s%s%d%d%s%s%s%lld%s%lld", &id, name, sex, &age, &tall,sf, price,introduce,&phone,email,&QQ);

	printf("%d%s%s%d%d%s%s%s%lld%s%lld\n", id, name, sex, age, tall,sf, price, introduce, phone, email, QQ);
	char *pos ;
	printf("%s\n", price);
	char pr[] = "100岁年龄。";
	//strtol:从前向后读整数,遇到非数字终止,pos:为找到第一个非数字的字符地址、10:表示进制
	//为什么strtol() 第二个参数需要传递 地址,因为改变一个指针变量的值,需要指针变量的地址。
	int vale = strtol(pr, &pos, 10);
	printf("%d %s", vale, pos);
	system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值