We help IT Professionals succeed at work.

Passing paramters to another program in C ?

triz99
triz99 asked
on
450 Views
Last Modified: 2010-04-15
I have the fallowing program with code something like this

#include<stdio.h>
#include<unistd.h>

int main()
{
  char test[255];
  printf("write here: ");
  scanf("%s", test);
  return 0;
}  

My question is how to pass some data into test variable from another program

I now how to do it when program use argv/argc, with system("./program blabla-data"), but my question is how to pass it into the test variable.

It must work under Linux.

Thank you
Comment
Watch Question

Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
Hi triz99,

You can't directly pass data from one program to another by setting variable in memory.  Each program has its own memory and doesn't allow other programs to write into it.

You'll have to open a link between the two programs and exchange information that way.  You can get as involved as a full client-server implementation, but that's clearly overkill.

Pipes will do nicely.  Check out the pipe() and popen() functions.  They can establish a comm link between programs so that you can use fgets() and fputs() to exchange data between the programs.


Good Luck,
Kent
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
do you know how to call system commands?

Here is how.

Now what you can do it, compile your other program as first.out and then  you can call that program from another program like this
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int res;

  puts ("Trying to execute command DIR");

  res = system ("./first.out arg1 arg2");

  if (res == -1)
      puts ("Error executing DIR");
  else
      puts ("Command successfully executed");

  return 0;
}


In your first program you can get these arg1 and arg2 and assign it to test variable.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Chapter 5 discusses more than just shared memory. It is all about interprocess communications.

Commented:
You can use one of the following:
(1)  File
(2) Shared memory
(3) messaging..

Commented:
Ex for using argc/argv in a program

int
main(int argc,
        char *argv[])
{
    char test[255];
    if (argc >= 2) {
        strcat(test, argv[1]);
    }
    printf("%s", test);
}

I check if argc is equal or larger  the 2 to make sure that there are arguments into the program (argv[0] is equal to the program that is started)

As for launching other programs you can use execv(char *path, char *argv[])
path is the application you want to start.
argv is a list of arguments that will end up in argv for that program (remember that argv[0] should always contain the program you are launching in path)

ex. of launching process in linux

int
main(int argc,
         char *argv[])
{
    char *next_program_args[3];
    next_program_args[0] = strdup("/usr/bin/less");
    next_program_args[1] = strdup("/etc/passwd");
    next_program_args[2] = NULL; /* This is to indicate to next progrma that there are no more args*/

    execv(next_program_args[0], next_program_args);
}

As for includes needed for these examples use man for the different calles to get the info

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.