开发者

Sscanf problem and reading from command-line

开发者 https://www.devze.com 2023-01-29 23:21 出处:网络
Hello hahah yet again. C开发者_StackOverflow- Unix Im reading something like this: a.out [23+10i] sscanf(argv[count], \"[%d%c%di]\", &a, &operand1, &b);

Hello hahah yet again. C开发者_StackOverflow- Unix Im reading something like this:

a.out [23+10i]

sscanf(argv[count], "[%d%c%di]", &a, &operand1, &b);

I want to separate as follows: a=23 b=10 operand='+' but im getting that segfault(core dumped) error, what am I doing wrong?

By the way, thanks alot for answering all my silly questions


It works for me

#include <stdio.h>
int main(int argc, char **argv) {
    int a, b;
    char operand1;
    int n;

    printf("scanning argv[argc - 1]: %s\n", argv[argc - 1]);
    /* It works for me */
    n = sscanf(argv[argc - 1], "[%d%c%di]", &a, &operand1, &b);

    printf("%d values read\n", n);
    if (n != 3) printf("there was an error in scanf ...\n");
    printf("The values were %d, %c, and %d\n", a, operand1, b);
    return 0;
}

And a sample run:

$ ./a.out [23+43i]
scanning argv[argc - 1]: [23+43i]
3 values read
The values were 23, +, and 43

Note that argv[argc] is a NULL pointer.

0

精彩评论

暂无评论...
验证码 换一张
取 消