User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 426,810 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,920 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 628 | Replies: 5 | Solved
Reply
Join Date: Dec 2007
Posts: 144
Reputation: TheBeast32 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Junior Poster

popen

  #1  
Jul 24th, 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 24th, 2008 at 11:17 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,638
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 22
Solved Threads: 418
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: popen

  #2  
Jul 25th, 2008
Better check your popen() manual, because most implementations are uni-directional.
Either read-only, or write-only.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Dec 2007
Posts: 144
Reputation: TheBeast32 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Junior Poster

Re: popen

  #3  
Jul 25th, 2008
That's weird, because if you type help, you will get the help message.
"I'm not dumb. I just have a command of thoroughly useless information."
- Calvin, of Calvin and Hobbes
Reply With Quote  
Join Date: Dec 2007
Posts: 144
Reputation: TheBeast32 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Junior Poster

Re: popen

  #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.
"I'm not dumb. I just have a command of thoroughly useless information."
- Calvin, of Calvin and Hobbes
Reply With Quote  
Join Date: Jan 2008
Posts: 1,771
Reputation: VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice VernonDozier is just really nice 
Rep Power: 8
Solved Threads: 218
VernonDozier VernonDozier is offline Offline
Posting Virtuoso

Re: popen

  #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?
Reply With Quote  
Join Date: Dec 2007
Posts: 144
Reputation: TheBeast32 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Junior Poster

Re: popen

  #6  
Jul 26th, 2008
I think you're right. I changed the command to help. That displayed the entire thing. Thanks for the help!
"I'm not dumb. I just have a command of thoroughly useless information."
- Calvin, of Calvin and Hobbes
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC