| | |
popen
Please support our C++ advertiser: Intel Parallel Studio Home
Thread 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 25th, 2008 at 12:17 am.
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);
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
--Martin Golding
•
•
Join Date: Jan 2008
Posts: 3,820
Reputation:
Solved Threads: 501
•
•
•
•
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? Maybe I'm confused about the goal of the program.
![]() |
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
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






