•
•
•
•
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
![]() |
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.
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.
C++ Syntax (Toggle Plain Text)
#include <windows.h> #include <stdio.h> #include <conio.h> #include <string> #include <sstream> #include <iostream> using namespace std; int main() { FILE *fp = popen("cmd.exe", "r+" ); char buff[50]; char command[50]; while (fp != NULL) { while ( fgets( buff, sizeof buff, fp ) != NULL ) { cout << buff; } cin.getline(command, sizeof command); fputs(command, fp); } pclose( fp ); getch(); }
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.
Better check your popen() manual, because most implementations are uni-directional.
Either read-only, or write-only.
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.
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.
I modified it a little:
It's better, but I still can't get the ouput correctly.
C++ Syntax (Toggle Plain Text)
strcat(command, "\n"); fputs(command, fp);
"I'm not dumb. I just have a command of thoroughly useless information."
- Calvin, of Calvin and Hobbes
- Calvin, of Calvin and Hobbes
•
•
Join Date: Jan 2008
Posts: 1,771
Reputation:
Rep Power: 8
Solved Threads: 218
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#include <windows.h> #include <stdio.h> #include <conio.h> #include <string> #include <sstream> #include <iostream> using namespace std; int main() { FILE *fp = popen("cmd.exe", "r+" ); char buff[50]; char command[50]; while (fp != NULL) { while ( fgets( buff, sizeof buff, fp ) != NULL ) { cout << buff; } cin.getline(command, sizeof command); fputs(command, fp); } pclose( fp ); getch(); }
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?
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- os.popen (Python)
- Weird segfault involving popen (C)
Other Threads in the C++ Forum
- Previous Thread: Flow charts for currency conversion
- Next Thread: listView1



Linear Mode