Please Help!!!

This is a discussion on Please Help!!! within the C Programming forums, part of the General Programming Boards category; Please Help!! How can I send information and receive from external command prompt from my program??. I want to run ...

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    1

    Please Help!!!

    Please Help!!

    How can I send information and receive from external command prompt from my program??.
    I want to run an external command prompt, a code that's is not mine, to send information to this command prompt, then make my program hit enter at
    the command prompt and then receive back information from the command prompt.

    Thanks in advanced.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    You can use system to run external commands.

    Man Page for SYSTEM (freebsd Section 3) - The UNIX and Linux Forums

    If your aim is to send and receive information interactively from two separate processes, then that will require more care, perhaps by using the exec family of functions and something like fork.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The pro way is to use pipes.
    But if you're looking for a simple way, you could direct the output of the called program to a file. (Here I'm assuming you're on windows since you used the term "command prompt".)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main() {
        char *name = tmpnam(NULL);
        if (!name)
            return EXIT_FAILURE;
    
        char command[L_tmpnam + 10] = "dir > ";
        strcat(command, name);
    
        system(command);
    
        char line[BUFSIZ];
        FILE *f = fopen(name, "r");
        while (fgets(line, sizeof line, f)) {
            if (strstr(line, "<DIR>"))
                fputs(line, stdout);
        }
        fclose(f);
    
        remove(name);
    
        return 0;
    }
    Last edited by oogabooga; 01-10-2014 at 03:41 PM. Reason: changed TMP_MAX to L_tmpnam
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    @yaron:
    This is OS specific. Assuming you're on Linux, pipe-fork-exec is a pretty standard way to communicate with the stdin and stdout of another program. The man page for pipe() has a nice example. If you're on Windows, this link might help.

    @oogabooga:
    From the man page of tmpnam
    Quote Originally Posted by man 3 tmpnam
    Bugs

    Never use this function. Use mkstemp(3) or tmpfile(3) instead.
    IIRC, there's some timing issues/race condition with when it checks if the name is available and when the name is actually created in the file system, leaving the possibility of two things using the same temp file.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by anduril462 View Post
    IIRC, there's some timing issues/race condition [with tmpnam] with when it checks if the name is available and when the name is actually created in the file system, leaving the possibility of two things using the same temp file.
    Surely this is system-specific.
    tmpfile would not work here.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by oogabooga View Post
    Surely this is system-specific.
    tmpfile would not work here.
    Ahh, I always forget tmpnam is a standard function. But so is tmpfile actually, though you have no control over where you put it. mkstemp is POSIX though.

    So yes, that bug is Linux/POSIX only. And yes, tmpfile is not the right thing here, since he can't get the name.

Popular pages Recent additions subscribe to a feed

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