简体   繁体   English

C:检查字符串是否为整数并保存

[英]C: Check if a string is an integer and save it

I've been searching the internet for some time, but didn't find a simple solution for a actually simple problem in my eyes. 我已经在互联网上搜索了一段时间,但在我眼中却没有找到解决一个实际简单问题的简单解决方案。 I guess it has been asked already: 我想已经有人问过了:

I'm reading a value like 20.1 or XYZ via sscanf from a file and saving it in char *width_as_string . 我正在通过sscanf从文件中读取20.1XYZ类的值,并将其保存在char *width_as_string

All functions should be valid in -std=c99 . 所有功能在-std=c99均应有效。

Now I want to check if the value in width_as_string is an integer. 现在,我要检查width_as_string的值是否为整数。 If true, it should be saved in int width . 如果为true,则应将其保存为int width If false, width should remain with the value 0 . 如果为false,则width值应保持为0

My approaches: 我的方法:

int width = 0;
if (isdigit(width_as_string)) {
    width = atoi(width_as_string);
}

Alternatively, convert width_as_string to int width and convert it back to a string. 或者,将width_as_string转换为int width并将其转换回字符串。 Then compare if it is the same. 然后比较是否相同。 But I'm not sure how to achieve that. 但是我不确定如何实现这一目标。 I already tried itoa . 我已经尝试过itoa

Functions like isdigit and itoa are not valid in std=c99 , therefore I can't use them. isdigititoa这样的函数在std=c99无效,因此我不能使用它们。

Thanks. 谢谢。

How about strtol? 怎么样呢?

This gives a clear return value if something goes wrong, i think this is what you're looking for 如果出现问题,这将提供明确的返回值,我认为这就是您要寻找的

http://www.cplusplus.com/reference/cstdlib/strtol/ http://www.cplusplus.com/reference/cstdlib/strtol/

Actually, you could use sscanf at the very beginning to check whether the number is integer or not. 实际上,您可以在一开始使用sscanf来检查数字是否为整数。 Something like this 像这样

 #include <stdio.h>
 #include <string.h>

 int 
 main (int argc, char *argv[])
 {
    int wc; // width to check
    int w; // width

    char *string = "20.1";

    printf("string = %s\n", string);

    if (strchr(string, '.') != NULL)
    {
        wc = 0;
        printf("wc = %d\n", wc);
    }
    else if ((sscanf(string, "%d", &w)) > 0)
    {
        wc = w;
        printf("wc = %d\n", wc);    
    } else w = 0;

    return 0;
}

This is a sample program of course, it first searches the string for a "." 当然,这是一个示例程序,它首先在字符串中搜索“”。 to verify if the number could be float and discards it in such a case, then tries to read an integer if no "." 验证数字是否为浮点数,并在这种情况下将其丢弃,然后尝试读取整数(如果没有为“。”)。 are found. 被发现。

Changed thanks to ameyCU's suggestion 由于ameyCU的建议而改变

Reference page for sscanf sscanf的参考页

Read carefully some documentation of sscanf . 仔细阅读sscanf一些文档 It returns a count, and accepts the %n conversion specifier to give the number of character (bytes) scanned so far. 它返回一个计数,并接受%n转换说明符以提供到目前为止已扫描的字符数(字节)。 Perhaps you want: 也许您想要:

int endpos = 0;
int width = 0;
if (sscanf(width_as_string, "%d %n", &width, &endpos)>=1 && endpos>0) {
  behappywith(width);
};

Perhaps you want also to add && width_as_string[endpos]==(char)0 (to check that the number is perhaps space suffixed, then reaching the end of string) after endpos>0 也许您还想在endpos>0之后添加&& width_as_string[endpos]==(char)0 (以检查数字是否有空格后缀,然后到达字符串的末尾)。

You could also consider the standard strtol which sets an end pointer: 您还可以考虑设置结束指针的标准strtol

char*endp = NULL;
width = (int) strtol(width_as_string, &endp, 0);
if (endp>width_as_string && *endp==(char)0 && width>=0) {
  behappywith(width);
}

The *endp == (char)0 is testing that the end of number pointer -filled by strtol - is the end of string pointer (since a string is terminated with a zero byte). *endp == (char)0正在测试由strtol填充的数字指针的末尾是字符串指针的末尾(因为字符串以零字节终止)。 You could make that more fancy if you want to accept trailing spaces. 如果您想接受尾随空格,可以更花一些钱。

PS. PS。 Actually, you need to specify precisely what is an acceptable input (perhaps by some EBNF syntax). 实际上,您需要精确指定什么是可接受的输入(也许通过某些EBNF语法)。 We don't know if "1 " or "2!" 我们不知道是"1 "还是"2!" or "3+4" are (as C strings) acceptable to you. "3+4" (作为C字符串)是您可以接受的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM