scanf的格式说明只读一些 [英] scanf format specifier to only read a number

查看:131
本文介绍了scanf的格式说明只读一些的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我的输入值是:

 2A,6,-20,+20

我想读下面的值转换成整数:

  0,6,-20,20

我目前使用这条线code的:

 的sscanf(输入%d个,&安培; some_integer);

但它读取2A为具有2的整数值 - ?我怎么能读2A为0


解决方案

您可以做到这一点却

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;INT
主要()
{
    字符文本[100];
    int值;
    诠释计数;
    字符* endptr;    炭源[] =2A 6 -20 +20
    字符*输入;    输入=源;
    而(sscanf的(输入%99S%N,文字和放大器;计数)== 1)
    {
        值=与strtol(文字,和放大器; endptr,10);
        如果(* endptr!='\\ 0')
            值= 0;
        输入+ =计数;        的printf(%D,值);
    }
    的printf(\\ n);    返回0;
}


  1. 使用的sscanf()%N说明知道有多少个字符,其中阅读和推进指针继续读书。

  2. 使用与strtol()来读值转换为整型,如果不是可转换的第一个字符是转换停止,请注意,参数是非常重要的,因为 2A 是一个有效的十六进制数。

  3. 检查转换成功,如果所有的字符转换的地方, * endptr =='\\ 0' whill满意。

  4. 推进指针源字符串,并继续下去,直到没有更多的字符是左的阅读。

如果你需要的是检查,如果输入的字符串是一个数字,这是你应该用什么

  INT isdecimal(常量char * const的文字)
{
    字符* endptr;
    如果(输入== NULL)
        返回0;
    / * 10是表示该数字是十进制再presentation * /
    strtol将(文字,&放大器; endptr,10);
    返回(* endptr =='\\ 0')
}

如果您只想值为 0 如果不是敞篷车,那么这是不够

 转换为int(为const char * const的输入)
{
    int值;
    字符* endptr;    值= strtol将(输入,&放大器; endptr,10);
    如果(* endptr!='\\ 0')
        值= 0;
    返回值;
}

Say my input values are:

"2a", "6", "-20" "+20"

I want to read the following values into an integer:

0, 6, -20, 20

I'm currently using this line of code:

sscanf(input, "%d", &some_integer);

but it reads "2a" as having an integer value of 2 - How can I read "2a" as 0?

解决方案

You can do this however

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

int
main()
{
    char  text[100];
    int   value;
    int   count;
    char *endptr;

    char source[] = "2a 6 -20 +20";
    char *input;

    input = source;
    while (sscanf(input, "%99s%n", text, &count) == 1)
    {
        value = strtol(text, &endptr, 10);
        if (*endptr != '\0')
            value = 0;
        input += count;

        printf("%d ", value);
    }
    printf("\n");

    return 0;
}

  1. Use sscanf() and the "%n" specifier to know how many characters where read and advance the pointer to continue reading.
  2. Use strtol() to convert the read value to integer, the conversion stops when the first character that is not convertable is found, note that the base argument is very important because 2A is a valid HEX number.
  3. Check that the conversion succeeded, if all the characters where converted, *endptr == '\0' whill be satisfied.
  4. Advance the pointer to the source string, and continue until no more characters are left for reading.

If all you need is to check if the input string is a number, this is what you should use

int isdecimal(const char *const text)
{
    char *endptr;
    if (input == NULL)
        return 0;
    /* the 10 is to indicate that the number is in decimal representation */
    strtol(text, &endptr, 10); 
    return (*endptr == '\0')
}

if you just want the value to be 0 if it's not convertible, then this is enough

int convert(const char *const input)
{
    int   value;
    char *endptr;

    value = strtol(input, &endptr, 10);
    if (*endptr != '\0')
        value = 0;
    return value;
}

这篇关于scanf的格式说明只读一些的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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