Thread: output of an other programm into a variable

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    output of an other programm into a variable

    hello,

    how do i put the output of another program into a variable of my c-programm?
    i have tried it this way, but it does not work:

    Code:
    system(StringOfAskingTheOtherProgram)
    scanf("%s", strMyVariable);
    does anyone know?

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Yah you can do that did you heard of send message technique in Windows.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I dont know if this will be useful or not but you can #include the code and use the variable(which is your output) using extern keyword.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    I'm using Linux.
    Maybe it would work with unix-pipes? i don't know how.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    Pipes indeed. This is an example:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    int main(){
    	int c;
    	FILE * ifp;
    	ifp = popen("ls", "r");
    	while ((c = getc(ifp)) != EOF)
    		putchar(toupper(c));
    	pclose(ifp);
    	return 0;
    }
    This program takes the output of the linux command "ls" in "r"-mode (this is a file-mode, meaning 'read') and puts the output in upper case. All the magic is in the popen()
    Hope this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-14-2009, 03:15 AM
  2. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  3. Replies: 4
    Last Post: 07-02-2004, 04:08 PM
  4. why this output?
    By dredre in forum C Programming
    Replies: 4
    Last Post: 05-08-2004, 04:09 PM
  5. Store command output into a variable.
    By Kevin.j in forum C Programming
    Replies: 7
    Last Post: 09-29-2002, 11:44 AM