Call external C programs and receive data

This is a discussion on Call external C programs and receive data within the C Programming forums, part of the General Programming Boards category; Hi, I'd like to know if I can call a program from a program and receive result. I've this complied ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    Call external C programs and receive data

    Hi,

    I'd like to know if I can call a program from a program and receive result.

    I've this complied C program (Called):
    Code:
    /* omissis header */
    
    int main()
    {
      int i = 0;
      i = rand();
      return i;
    }
    Can I call this program from another C program and receive "i" ?

    My pseudo-code would be:
    Code:
    /* omissis */
    
     int dest = 0;
     dest = call Called program;
    
    /* omissis */
    Does the "return" instruction pass data also through compiled programs?
    How to do it? How to call external programs?

    Thanks

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    984
    You can't do this portably.

    On unix, system() (which is a standard function) will return the termination status of whatever you're running. But remember that you only get 8 bits for the return value, so you can only return values 0-255.

    Again assuming unix, you could have one program print out a number and then the other program use popen() to grab the output and parse it. This is somewhat analogous to using `` or $() in the Bourne shell; it just takes a bit more work to do.

    I'm not familiar with other platforms so I can't comment on them.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    ShellExecuteEx and GetExitCodeProcess can do this for Windows.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    ShellExecuteEx and GetExitCodeProcess can do this for Windows.
    popen() also works on Windows.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    The 'best' way on Windows I'd say is, CreateProcess() with inherited pipes (stdout, stdin or stderr).

    That means you can send more than just 1 thing, or just a number.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,041
    system() should be sufficient for getting the error/return code of the process.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    ?

    And where is the result stored?

    Thanks (sorry, I'm new with C)

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    System returns what is returned by the program it executes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,041
    Quote Originally Posted by ferenczi View Post
    And where is the result stored?

    Thanks (sorry, I'm new with C)
    System returns an int.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    But, I mean, if I want to use this result, where do I find it. Have I to use something like this:

    Code:
    int take = 0;
    take = system("extern.exe");
    ?

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by ferenczi View Post
    But, I mean, if I want to use this result, where do I find it. Have I to use something like this:

    Code:
    int take = 0;
    take = system("extern.exe");
    ?
    In the example you gave, you'd find the result in the take variable. So whatever value main() returned in extern.exe is what take should equal.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by cpjust View Post
    In the example you gave, you'd find the result in the take variable. So whatever value main() returned in extern.exe is what take should equal.
    Except the case when was error starting the external program. In any case - you should read the description of system function in C-reference, especially - the return value.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hacking a server program
    By adr in forum Networking/Device Communication
    Replies: 9
    Last Post: 02-22-2008, 01:46 PM
  2. Error in copying of charachters
    By ferenczi in forum C Programming
    Replies: 6
    Last Post: 02-09-2008, 02:59 PM
  3. GZIP Data
    By cx323 in forum C# Programming
    Replies: 1
    Last Post: 06-22-2006, 07:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21