c - 如何获取系统(program.exe)的输入?

标签 c

我是 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.exeargv[0]2argv[1] 3argv[2]。阅读 atoi引用以了解它的作用。

关于c - 如何获取系统(program.exe)的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19631212/

相关文章:

c - fscanf 如何将 20.20.2012 之类的日期导入一个变量?

c - 对 `memcached_exist' 的 undefined reference

c - 尝试调整二维数组大小时出现段错误

c - redisAsyncConnect() 与 redisConnect() 有何不同?

c - 打印堆的所有叶子

c - Strlen 导致段错误

c++ - 公开MP瓶颈问题

c - 我在使用 C 中的函数指针数组时遇到问题

c - 为什么这个指针出现段错误 C?

c - MPI 发送数组数组