简体   繁体   English

如何从命令行读取参数到双精度?

[英]How to read an argument in from command line into a double?

This program is first forked then run by execlp, it's calling program passes in two numbers, a power and a base. 这个程序首先分叉然后由execlp运行,它的调用程序传递两个数字,一个幂和一个基数。

int main(int argc, char *argv[])
{
    int pid = getpid();
    printf("Calculator Process[%d]: started\n",pid);
    double base, power;
    sscanf(argv[1],"%d",&base);
    sscanf(argv[2],"%d",&power);
    double number = pow(base,power);
    printf("Calculator Process[%d]: %d ^^ %d == %d\n",pid,base,power,number);
    printf("Calculator Process[%d]: exiting\n",pid);
    return 1;
}

Lets say I pass into it base 3, power 5. This is what I get: 让我们说我进入它的基础3,力量5.这就是我得到的:

base = 4263 -- this also happens to be the PID. 
power = -1 
raised to power: 714477568

Calling line: 通话线路:

execlp("./calculator","./calculator",argv[1],argv[2],(char*)0);

When I print the argvs, I get their value (as a char*, but casting fails). 当我打印argvs时,我得到它们的值(作为char *,但是转换失败)。

Any ideas why I can't get the values to be correctly read in? 任何想法为什么我不能正确读入值?

Either read double: 阅读双倍:

double base, power;
sscanf(argv[1],"%lf",&base);
sscanf(argv[2],"%lf",&power);

Or scan into integers: 或者扫描成整数:

int base, power;
sscanf(argv[1],"%d",&base);
sscanf(argv[2],"%d",&power);

A quick adjustment of your code should do it: 快速调整代码应该这样做:

int main(int argc, char *argv[]) 
{
    int pid = getpid();
    printf("Calculator Process[%d]: started\n",pid);
    double base, power;
    base = atof(argv[1]);
    power = atof(argv[2]);
    double number = pow(base,power);
    printf("Calculator Process[%d]: %f ^^ %f == %f\n",pid,base,power,number);
    printf("Calculator Process[%d]: exiting\n",pid);
    return 1; 

} }

  1. For converting a char array to a digit you can use atof() 要将char数组转换为数字,可以使用atof()
  2. Don't forget to update the printf to a double %f or they won't display right in your output! 不要忘记将printf更新为double %f否则它们将无法在输出中显示!

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

相关问题 如何从C中的命令行参数中读取“string”? - How to read “string” from command line argument in C? 如何从C中的命令行读取* - How to read * from command line in C 如何从命令行读取文件 - How to read file from the command line C语言帮助:如何将.txt文件中的字符串存储到字符数组中? 从命令行参数btw读取此.txt文件。 - C language help:How to store strings from a .txt file into a character array? This .txt file is read from a command line argument btw. 我可以将命令行参数读为二进制文件吗? - Can I read command line argument as binary? 如何在C中获取命令行参数并评估为int或double时检查边缘情况? - How to check an edge case in taking command line argument in C and evaluating to int or double? 如何在取自命令行参数的字符串中打印转义字符? - how to print escape character in string taken from command line argument? 如何使用命令行参数替换文件中的fread()? - How to replace fread() from file, with command line argument? 如何获取从命令行参数调用的文件的目录? - How to get directory of a file which is called from command line argument? 从命令行参数中读取随机数的 textfile.txt 并找到总和 - Read a textfile.txt of random numbers from the command line argument and find the sum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM