我是 C 初学者。我有一个程序,它使用 system("subprogram.exe")
运行另一个简单的加法程序。功能。现在这个子程序有两个整数输入,我怎样才能将输入提供给 subprogram.c
来 self 的主程序的文件。
主要程序:
#include<stdio.h>
int main()
{
int a,b;
a=10;
b=10;
system("subprogram.exe");
}
现在子程序有以下代码..
#include<stdio.h>
int main()
{
int a,b,c;
c=a+b;
printf("%d",c);
return 0;
}
如何将“主程序”中“a”和“b”的值复制到“子程序”?
最佳答案
简单回答命令行参数。
您的主程序:
#include<stdio.h>
#include<string.h>
int main()
{
int a,b;
a=10;
b=10;
char str1[10], str2[10];
char progCmdline[100];
sprintf(str1, " %d", a); //Convert int a to string str1
sprintf(str2, " %d", b); //Convert int b to string str2
strcpy(progCmdline,"subprogram.exe "); //Build
strcat (progCmdline,str1); // Your command line string
strcat (prog,str2); // with inputs
system(progCmdline);
}
您的子程序:
#include<stdio.h>
int main(int argc, char **argv )
{
int a,b,c;
if(argc>0)
{
a = atoi(argv[1]);
b = atoi(argv[2]);
c = a + b;
printf("%d",c);
}
return 0;
}
subprogram.exe
为 argv[0]
,2
为 argv[1]
, 3
是 argv[2]
。阅读 atoi引用以了解它的作用。
关于c - 如何获取系统(program.exe)的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19631212/