popen

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

popen

 
0
  #1
Jul 25th, 2008
Hi, how would I make a program that gets input from a user, sends it to a program with popen(), gets the output, and repeats this until the program ends? I have tried, but I only get part of the output, and when I type "exit", it won't end the program.

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string>
  5. #include <sstream>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.  
  12. FILE *fp = popen("cmd.exe", "r+" );
  13. char buff[50];
  14. char command[50];
  15.  
  16. while (fp != NULL)
  17. {
  18. while ( fgets( buff, sizeof buff, fp ) != NULL )
  19. {
  20. cout << buff;
  21. }
  22. cin.getline(command, sizeof command);
  23. fputs(command, fp);
  24. }
  25. pclose( fp );
  26.  
  27. getch();
  28.  
  29. }

From this, I only get "Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp." when it starts, while I should also be getting the "C:\documents and settings\...." after it.
Last edited by TheBeast32; Jul 25th, 2008 at 12:17 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: popen

 
0
  #2
Jul 25th, 2008
Better check your popen() manual, because most implementations are uni-directional.
Either read-only, or write-only.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: popen

 
0
  #3
Jul 25th, 2008
That's weird, because if you type help, you will get the help message.
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: popen

 
0
  #4
Jul 25th, 2008
I modified it a little:

  1. strcat(command, "\n");
  2. fputs(command, fp);
It's better, but I still can't get the ouput correctly.
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,820
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: popen

 
0
  #5
Jul 25th, 2008
Originally Posted by TheBeast32 View Post
Hi, how would I make a program that gets input from a user, sends it to a program with popen(), gets the output, and repeats this until the program ends? I have tried, but I only get part of the output, and when I type "exit", it won't end the program.

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string>
  5. #include <sstream>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.  
  12. FILE *fp = popen("cmd.exe", "r+" );
  13. char buff[50];
  14. char command[50];
  15.  
  16. while (fp != NULL)
  17. {
  18. while ( fgets( buff, sizeof buff, fp ) != NULL )
  19. {
  20. cout << buff;
  21. }
  22. cin.getline(command, sizeof command);
  23. fputs(command, fp);
  24. }
  25. pclose( fp );
  26.  
  27. getch();
  28.  
  29. }

From this, I only get "Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp." when it starts, while I should also be getting the "C:\documents and settings\...." after it.

I don't think you're supposed to get "C:\documents and settings\...." when you run this. That's the command line prompt, not part of the program. The output I got from the program was the same as when I type "cmd" in at the command prompt. Same thing when I wrote a little "Hello World" program and replaced "cmd.exe" with "HelloWorld.exe". It just displayed "Hello World". Is it supposed to display the command prompt before the program exits? Maybe I'm confused about the goal of the program.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: popen

 
0
  #6
Jul 26th, 2008
I think you're right. I changed the command to help. That displayed the entire thing. Thanks for the help!
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC