Fehler im String?
-
Hallo,
möchte die IP's und die Hosts im Netz abfragen und habe mir sowas ausgedacht: ich lasse zuerst ein "net view" ausführen schreibe die Rechner in eine Datei namens hosts.txt und lass dann ein "ping" zu diesen Rechnern machen um die IP herauszubekommen. Dir hosts.txt Datei sieht so aus:
PC1
Pc2Mein Konstrukt sieht so aus:
ifstream infile_host("hosts.txt"); ofstream outfile; while(!infile_host.eof()){ getline(infile_host,pc_name); pings << "ping -a -n 1 -l 0.1 -w 0.1 " << pc_name << " > ping.txt" << endl; system(pings.str().c_str()); cout << pc_name << endl; } infile_host.close();Das Problem ist, es erkennt PC2 an der Stelle irgendwie nicht. In der ping.txt Datei steht immer der ping zu PC1. Durch das cout kann ich sehen, dass er die beiden PC's liest und auflistet im promt.
Wieso das so ist, verstehe ich nicht. Kann mir einer helfen?
Danke
-
Versuchs mal so:
ifstream infile_host("hosts.txt"); ofstream outfile; while(getline(infile_host,pc_name)){ // Die alte while-Bedingung war falsch. stringstream pings << "ping -a -n 1 -l 0.1 -w 0.1 " << pc_name << " > ping.txt" << endl; // Mach mal lieber den stringstream lokal, dann ist er auch immer leer. system(pings.str().c_str()); cout << pc_name << endl; } infile_host.close();
-
SeppJ schrieb:
Versuchs mal so:
ifstream infile_host("hosts.txt"); ofstream outfile; while(getline(infile_host,pc_name)){ // Die alte while-Bedingung war falsch. stringstream pings << "ping -a -n 1 -l 0.1 -w 0.1 " << pc_name << " > ping.txt" << endl; // Mach mal lieber den stringstream lokal, dann ist er auch immer leer. system(pings.str().c_str()); cout << pc_name << endl; } infile_host.close();Warum war die Bedingung falsch? Ich empfand die als richtig.
-
so hats geklappt, danke.
ifstream infile_host("hosts.txt"); ofstream outfile; while(getline(infile_host,pc_name)){ // Die alte while-Bedingung war falsch. stringstream pings; pings << "ping -a -n 1 -l 0.1 -w 0.1 " << pc_name << " > ping.txt" << endl; // Mach mal lieber den stringstream lokal, dann ist er auch immer leer. system(pings.str().c_str()); cout << pc_name << endl; } infile_host.close();Grüße