-1

我是 C 的初学者。我有一个程序使用system("subprogram.exe")函数运行另一个简单的加法程序。现在这个子程序有两个整数输入,我怎样才能subprogram.c从我的主程序给文件输入。

主程序:

#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'中的值从“主程序”复制到“子程序”?

4

6 回答 6

1

您需要使用命令行参数来简单地完成此操作。还有其他方法,比如进程间通信,但是如果你想从主程序中打开 subprogram.exe,那么你可以使用命令行参数。

代替

system("subprogram.exe");

利用

sprintf(command, "subprogram.exe %d %d", a, b)
system( command );

这会将您的整数与 subprogram.exe 连接起来,然后执行它。不要忘记声明命令字符数组。

但是,您还必须更改子程序主函数以获取参数。在执行程序时传递命令行参数时,参数的数量和参数数组将传递给主函数。

int main ( int argc, char *argv[] )
{
  if (argc < 2)  /* assuming that you need two inputs */
  {
          printf("needs atleast two inputs");
  }
  else
  {
         printf("%d",atoi(argv[1])+argv[2]));
  }
}
于 2013-10-28T09:46:58.357 回答
1

简单回答命令行参数。

你的主程序:

#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参考以了解它的作用。

于 2013-10-28T09:42:05.517 回答
0

简单来说是这样的:

主程序

#include<stdio.h>
#include <process.h>

int main()
{
 char command[100];

    int a,b;
    a=10;
    b=10;
    sprintf(command, "subprogram  %i %i" , a,b);
    system(command);
} 

子程序

 #include<stdio.h>
    int main(int argc, char** argv)
    {
        int a,b,c;
        a = atoi(argv[1]); // atoi() converts command line string type arrgument to int
        b = atoi(argv[2]);
        c=a+b;
        printf("%d",c);
        return 0;
    }

参考

atoi : 如何将字符串转换为 int 类型

命令行参数:如何传递和使用命令行参数

于 2013-10-28T10:04:17.380 回答
0

或者,您可以让主程序将这些内容保存到文件中,然后由子程序读取。

//In main program:
FILE * jakethedog = fopen("jakethedogfile.txt","w");
fprintf(jakethedog, "%d %d",a,b);
fclose(jakethedog);

//In subprogram:
FILE * cakethecat = fopen("jakethedogfile.txt","r");
fscanf(cakethecat,"%d %d",&a,&b);
fclose(cakethecat);

如果您想在主程序和子程序之间双向交换信息,我可以看到这更有用。

于 2013-10-28T09:55:54.453 回答
0

你有两种方法,

  1. 将值作为命令行参数传递。这是最容易的。你的主程序应该是-

    int main()
    {
       int a,b;
       a=10;
       b=10;
       char str[50];
       sprintf(str, "subprogram.exe %d %d", a, b);
       system(str);
    } 
    

和 subprogram.c-

include<stdio.h>
int main(int argc, char *argv[])
{
int a,b,c;
if(argc<3){
printf("not enough arguments");
exit();
}
a = atoi(argv[1]);
b = atoi(argv[2]);
c=a+b;
printf("%d",c);
return 0;
}

2.使用文件。 这里有一些教程,点击这里,这里,。更多关于谷歌搜索。

于 2013-10-28T09:49:51.127 回答
0

您将拥有任何进程间编程结构。您可以使用管道、消息队列或共享内存。你可以参考这个:http ://www.cs.cf.ac.uk/Dave/C/node23.html

于 2013-10-28T09:37:06.377 回答