Question about _popen

This is a discussion on Question about _popen within the C++ Programming forums, part of the General Programming Boards category; After foraging about here and there, I found a way to run a subprocess from my program (if that's the ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Question about _popen

    After foraging about here and there, I found a way to run a subprocess from my program (if that's the right way to call it).
    All in all, it runs the way I want it to, but there's a slight tick with it that I want to get rid of.
    If I want to display the output from the subprocess, I can't write to it, but if I write to it, I can't get output from it.

    Here's the offending line of code:
    Code:
    if( (pPipe = _popen( "xfst", "wr" )) == NULL )
          exit( 1 );
    If I change the ordering of the second argument in _popen to "rw", I can't pass input to it. It's fine the way it is right now, but I would like to see what's going on, while I'm at it.

    Here's the entire body of the code:
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    
    using namespace std;
    
    int main()
    {
       char   psBuffer[128];
       FILE   *pPipe;
    
       if( (pPipe = _popen( "xfst", "wr" )) == NULL )
          exit( 1 );
    
       while( !strcmp("\n", psBuffer))
       {
          if( fgets( psBuffer, 128, pPipe ) != NULL )
             printf( psBuffer );
       }
    	fprintf(pPipe, "define A [a|b]*;");
    	fprintf(pPipe, "exit\n");
    
    	printf( "\nProcess returned %d\n", _pclose( pPipe ) );
    	system("pause");
    	return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    popen() is a convenience function which restricts you to one-way communication.

    You need to dig deeper and find out the underlying mechanisms for inter-process communication.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thanks for letting me know it's impossible with popen.

    I just tried a google search with interprocess communication. It seems it's a field of its own (630,000 results).

    One thing I noticed was that the hits on the first page had to do with Unix or Linux and that piping was involved. I'm actually working with Windows, and as far as I know the Windows command line does not have piping built into it (unless you count the ">>" operator).

    Would it help me to sift through all that Unix information? If not, could you point me to a good reference site which is relevant to Windows (or OS neutral)?
    Last edited by cunnus88; 04-20-2006 at 01:07 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Since you used _popen(), I'm guessing windows
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    Some solution based around CreateProcess() would be my guess
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thanks. Hopefully, my brains will be up to the task.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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