Ausgabe der DOS Konsole in einer Variablen speichern
-
Hi..
Ich möchte gerne die Ausgabe an der Console in einer Variablen speichern..
Bsp:
Ich gebe "ipconfig /all" ein.. Die Ausgabe soll direkt in einer Variablen gespeichert werden, oder in mehreren, Zeile für Zeile.. Ich könnte zwar erst die Ausgabe in eine Datei umleiten und dann einlesen aber mich würde interessieren, ob es auch direkt geht.. Weiss jemand was darüber?
-
Man kann den stdout-Zeiger umlegen. Aber frag nicht wie
-
schau dir mal die Funktion _CrtSetReportFile() an, aber da kannst du glaube ich auch nur in Datei speichern
-
xCondoRx schrieb:
Ich möchte gerne die Ausgabe an der Console in einer Variablen speichern..
Welches BS verwendest du? Welche Klassenbibliothek?
In Standard-C++ gibt es keine schöne Lösung für dieses Problem.
Unter Unix/Linux und Win32-Konsole kannst du hingegen z.B. die Funktion popen verwenden.
-
Das Programm soll auf Windwos 2003 Server laufen.. Kannst du mir sagen, wo ich Informationen über die Funktion popen bekomme?
-
popen Syntax #include <stdio.h> FILE *popen(const char *program, const char *mode); Description This function executes the named program and attaches either its input stream or its output stream to the returned file. While the file is open, the calling program can write to the program (if the program was open for writing) or read the program's output (if the program was opened for reading). When the program is done, or if you have no more input for it, pass the file pointer to pclose (see section pclose), which terminates the program. Since MS-DOS does not support multitasking, this function actually runs the entire program when the program is opened for reading, and stores the output in a temporary file. pclose then removes that file. Similarly, when you open a program for writing, a temp file holds the data and pclose runs the entire program. The mode is the same as for fopen (see section fopen), except that you are not allowed to open a pipe for both reading and writing. A pipe can be open either for reading or for writing. Return Value An open file which can be used to read the program's output or write to the program's input. Portability not ANSI, POSIX
-
also es sieht jetzt ungefähr so aus:
int main() { char line[100]; FILE* returndata = _popen("ipconfig /all", "r"); fscanf(returndata,"%s",line); cout << line << endl; return 0; }
aber bei der Ausgabe kommt ja nur die erste Zeile.. Wie kann ich denn die anderen Zeilen anzeigen, bzw. in eine Variable speichern?
-
int main( ) { char line [100]; FILE* returndata = _popen("ipconfig /all", "r"); while( !feof( returndata ) ) { if( fgets( line, 100, returndata ) != NULL ) printf( line );//und hier halt in eine variable "konkaten" } printf( "\nProcess returned %d\n", _pclose( returndata ) ); }
-
Vielen Dank.. Hat super geklappt..