-1

I am beginner in C. I have a program which runs another simple addition program using system("subprogram.exe") function. Now this subprogram gets two integer inputs,how can I give input to the subprogram.c file from my main program.

Main Program:

#include<stdio.h>
int main()
{
    int a,b;
    a=10;
    b=10;
    system("subprogram.exe");
} 

Now subprogram has this following code..

#include<stdio.h>
int main()
{
    int a,b,c;
    c=a+b;
    printf("%d",c);
    return 0;
}

How to copy the values from 'a' and 'b' from "main program" to the "subprogram"?

taskinoor
  • 45,586
  • 12
  • 116
  • 142
seth
  • 1
  • 2
  • 2
    Asked and answered dozens of times. `man 3 popen`. –  Oct 28 '13 at 09:33
  • 1
    Standard C has no completely portable facility to communicate with a different process. You need to specify which platform you're on. – Fred Foo Oct 28 '13 at 09:34

6 Answers6

1

Simple answer command line arguments.

Your main program:

#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);
} 

Your subprogram:

#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 is argv[0], 2 is argv[1], 3 is argv[2]. Read atoi reference to understand what it does.

Sadique
  • 22,572
  • 7
  • 65
  • 91
  • He probably wants sprintf(buf, 10, "%d %d", a b)` to be the values passed to subprogram. Not `2 3`. – RedX Oct 28 '13 at 09:45
  • if i dont want to edit my subprogram as u have edited..then how to give input to it without passing any command line arguments or editing anything in my subprogram? – seth Nov 13 '13 at 07:10
  • The answer is simple you can't. – Sadique Nov 13 '13 at 07:16
  • just now i figured out to pass two inputs to subprogram without command line arguments. have a text file having two inputs say " 10 20" and i used this command system("subprogram.exe output.txt"). it works and the output is stored in the output text file which we can show it on the console right? – seth Nov 13 '13 at 11:28
  • Well there are various ways you could do things - but on the subject of your question - best would Cmd line args – Sadique Nov 13 '13 at 11:41
1

What you need to use is command line arguments to get this done simply. There are other methods such as interprocess communication, but if you want to open the subprogram.exe from main program, then you can use command line arguments.

instead of

system("subprogram.exe");

use

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

This will concatenate your integers with the subprogram.exe and then execute it. Don't forget to declare the command character array.

However you also have to change the subprogram main function to take arguments. When command line arguments are passed while executing a program, the count of number of arguments and the array of arguments will be passed to the main function.

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]));
  }
}
Buddha
  • 4,339
  • 2
  • 27
  • 51
0

You will have to you any Inter Process Programming constructs. You can use pipes, message queue or shared memory. You can refer this: http://www.cs.cf.ac.uk/Dave/C/node23.html

xboz
  • 174
  • 1
  • 8
0

You have 2 ways,

  1. Pass the values as command line arguments. This is the easy one. Your main program should be-

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

and 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. Use file. Here are some tutorials, click here, here, . More on google search.

muntasir2000
  • 204
  • 1
  • 3
  • 11
0

Alternately, you could have your Main Program save the stuff into a file, which will then be read by Subprogram.

//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);

I can see this being more useful if you want to exchange information between main program and subprogram in both directions.

Vincent
  • 319
  • 1
  • 11
0

In simple its like this:

Main Program

#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);
} 

Sub Program

 #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;
    }

Reference

atoi : How to convert string to an int type

command line arguments : How to pass and use command line arguments

Sid Ali
  • 1
  • 1
  • 2