如何使用sscanf说明符从一串文本中扫描一个数字? [英] How do I use sscanf specifiers to scan in a number from a string of text?

查看:84
本文介绍了如何使用sscanf说明符从一串文本中扫描一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在文本文件中的字符串末尾扫描一个int值。



字符串如下所示:



maxdifference = 7



我如何使用sscanf只读取7的值?



我尝试过:



I am trying to scan in an int value at the end of a string in a text file.

The string reads like this:

maxdifference = 7

How would i use sscanf to read the value of 7 only?

What I have tried:

sscanf(str, " %d", &settings->max_difference);

推荐答案

尝试:

Try:
char* str = "maxdifference = 7";
char string[100];
int val;
int filled = sscanf(str, "%s = %d", string, &val);
printf("read   : %d\ninteger: %d\n", filled, val);


这篇关于如何使用sscanf说明符从一串文本中扫描一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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