sscanf 直到它到达一个逗号 [英] sscanf until it reaches a comma

查看:51
本文介绍了sscanf 直到它到达一个逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从如下字符串中扫描单词和数字:hello, world, I, 287876, 6.0" <-- 此字符串存储在 char 中 数组(字符串)我需要做的是将事物拆分并将它们分配给不同的变量,因此就像

I'm trying to scanf words and numbers from a string looks like: "hello, world, I, 287876, 6.0" <-- this string is stored in a char array (string) What I need to do is to split things up and assign them to different variables so it would be like

     char a = "hello"
     char b = "world"
     char c = "I"
     unsigned long d = 287876
     float e = 6.0

我知道常规 scanf 在到达空白区域时会停止从 stdin 读取.所以我一直在想,当 sscanf 到达 "," (逗号)

I know that regular scanf stops reading from stdin when it reaches a white space. So I've been thinking that there might be a way to make sscanf stop reading when it reaches a "," (comma)

我一直在探索图书馆,以找到一种让 sscanf 仅读取字母和数字的格式.我找不到这样的东西,也许我应该再看一次.

I've been exploring the library to find a format for sscanf to read only alphabet and numbers. I couldn't find such a thing, maybe I should look once more.

有什么帮助吗?提前致谢:)

Any help? Thanks in advance :)

推荐答案

如果你的变量在字符串中的顺序是固定的,我的意思是它总是:

If the order of your variables in the string is fixe, I mean It's always:

string, string, string, int, float

sscanf()中使用以下格式说明符:

the use the following format specifier in sscanf():

int len = strlen(str);
char a[len];
char b[len];
char c[len];
unsigned long d;
float e;

sscanf(" %[^,] , %[^,] , %[^,] , %lu , %lf", a, b, c, &d, &e);

这篇关于sscanf 直到它到达一个逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆