Thread: Running a program

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    4

    Running a program

    Hello,
    I'm just starting to program in Unix and I want to execute a binary. The program that I execute gets user input from standard input and prints it out. How would I run the program then print to the standard in?

    I know that execl("./program"... ); works to run the program but I'm not sure how to give it input.

  2. #2
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Just type "./program_name" at your console. No need to use execl. To read input, use fscanf function, for example
    Code:
    #include <stdio.h>
    
    int main(){
    
       int n;
    
       printf("Enter a number\n");
    
       //read from standard input
       fscanf(stdin, "%d", &n);
    
       printf("Number entered is %d\n", n);
    
    
       return 0;
    
    }

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    Yes, but I need to be able to execute the program from within another program.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You would pass the program arguments. From the command line:
    Code:
    $ program arg1 arg2 argn
    In the program:
    Code:
    int main(int argc. char *argv[]) {
        int x;
    
        if(argc > 1) for(x = 1; x < argc; x ++) {
            printf("\nArgument %i: %s", x, argv[x]);
        }
        else printf("\nNo arguments passed\n");
    
        printf("\n");
    
        return 0;
    }
    I don't know how you run a program from another program in Linux. On my DOS machine it's system(), in stdlib.h. system() will probably work on your machine. But you can use execl() too if you want to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    system() is a standard function - it should work for any correct C/C++ implementation. It will just execute the string given to it in the system shell - and that's where the dangers starts. If you know that your program will only be run on your OS, than most of the problems become moot points.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    The problem is when you run System() it runs until the program ends, and if the program must have input then it will never end... Correct?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Perhaps popen is what you're looking for.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    Quote Originally Posted by quzah
    Perhaps popen is what you're looking for.

    Quzah.
    I tried popen, but it didnt give me what I wanted, it printed what I wanted to the screen, but it didn't input it to the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program running long
    By smarta_982002 in forum Windows Programming
    Replies: 3
    Last Post: 03-27-2008, 05:24 AM
  2. Running my program in the background
    By shoobsie in forum Windows Programming
    Replies: 4
    Last Post: 10-09-2005, 02:38 AM
  3. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  4. Why is my program running away?
    By badkitty in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2001, 04:27 PM
  5. Running program
    By muffin in forum C Programming
    Replies: 5
    Last Post: 08-30-2001, 10:57 AM