【问题标题】:Using strtol to filter bad command prompts and print error messages (C)使用 strtol 过滤错误的命令提示并打印错误信息 (C)
【发布时间】:2015-04-02 03:46:01
【问题描述】:

所以我编译程序并像 ./beetle int int 一样运行它,基本上是可执行文件和 2 个整数,我想测试可执行文件后面是否有超过 2 个参数,如果有一个参数是不是 int(如 300b)并且如果有一个参数是浮点数,而不是 int,并且如果参数溢出

我被困在如何对非整数和溢出进行测试,有人能指出我正确的方向吗?

   #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265
void beetleSimulation(int, int);


int main ( int argc, char *argv[] )
{
    char * pEnd;
    if ( argc != 3 ) // argc should be 2 for correct execution 
    {
        // If the number of arguments is not 2
        fprintf(stderr, "Error: Program has %d arguments ", argc);
        return 0;
    }
    else 
    {
       //run the beetle simulation
        beetleSimulation(strtol(argv[1], &pEnd, 10), strtol(argv[2], &pEnd, 10 ));
        if (!*pEnd)
            printf("Success!");
        else
            printf("Failed!");
        return 0;
        }
    }


void beetleSimulation(int size, int iterations){
    int i;
    double xCount;
    double yCount;
    int timeCount;
    int overallCount = 0;
    int degree;
    double radian;
    int border;
    for(i=0; i < iterations; i++){
        xCount = 0;
        yCount = 0;
        timeCount = 0;
        border -= size;
        while(xCount < size && xCount > border  && yCount <size && yCount >border ){
            timeCount += 1;
            degree = rand() % 360;
            radian = degree / (180 * PI);

            xCount += sin(radian);
            yCount += cos(radian);
            printf("Radian is %f\n", radian);

            printf("X and Y are %f and %f\n", xCount, yCount);
        }

        //when beetle has died, add time it took to overall count, then go through for loop again
        overallCount += timeCount;
        printf("Time for this run is %d, overall its %d\n",timeCount, overallCount);
    }
    //calculate average time
    double averageTime = overallCount/iterations;
    printf("Average Time is %f",averageTime);
}

【问题讨论】:

    标签: c warnings strtol


    【解决方案1】:

    建议获取 argv[x] 的 strlen() 值

    然后执行 strtol() 调用

    然后将 argv[x]+strlen() 的值与 strtol() 的第二个参数中设置的值进行比较

    如果它们匹配,则参数是一个很好的 int,

    否则,该论点(可能)不是一个好的 int。

    建议在调用 beetleSimulation() 之前进行所有这些比较

    由于对 strtol() 的两次调用都使用相同的变量作为结束地址,因此对 strtol() 的第二次调用将屏蔽第一次调用中设置的值。

    【讨论】:

    • 我有点困惑,有没有办法找到long的长度?
    【解决方案2】:

    字符串到int的转换函数。

    bool str2int(const char *s, int *dest) {
        char *endptr;
        errno = 0;
        long lnum = strtol(s, &endptr, 10);
        if (s == endptr) return 1;                               // fail - no conversion
        if (*endptr) return 1;                                   // fail - extra garbage
        if (errno || lnum < INT_MIN || lnum > INT_MAX) return 1; // fail - overflow
        *dest = (int) lnum;
        return 0; // success
      }
    

    【讨论】:

    • 如果参数不是 int,例如 40b 或 40.0,我不明白这将如何解释
    • @FullCombatBeard if (*endptr) return 1; endptr 指向停止strtol() 转换的char。对于“40b”,*endptr 指向 'b'。所以除非*endptr指向'\0',否则在转换后的long之后会有一些东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 2016-12-05
    • 1970-01-01
    • 2020-03-22
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多