stringstreams + arrays Problem
-
Hallo zusammen
Da ich immernoch ein Neuling in der C++ Programmierun bin, bitte ich um Verständnis.
#include <iostream> #include <dirent.h> #include <sstream> #include <string> using namespace std; int main(int argc, char *argv[]) { //counter int ipcount = 0; //plugin files char strplugfiles[100]; //dir handle DIR *dirHandle; struct dirent * dirEntry; dirHandle = opendir("."); if (dirHandle) { while (0 != (dirEntry = readdir(dirHandle))) { //don't show . and .. if(strcmp(dirEntry->d_name, ".") == 0 || strcmp(dirEntry->d_name, "..") == 0) { continue; } //cout << dirEntry->d_name << endl; stringstream strstrTemp1; strstrTemp1 << dirEntry->d_name; strplugfiles[ipcount] << strstrTemp1; cout << strplugfiles[ipcount] << endl; ipcount++; } closedir(dirHandle); } }Ich versuche verzweifelt die Ausgabe von dirEentry in den Array (strplugfiles) zu schreiben. Irgendwie habe ich es mit den Datentypen und ihren Eigenarten noch nicht so ganz.
Danke für Eure Hilfe.
-
Du möchtest wahrscheinlich die Filenamen innerhalb eines Directories abspeichern. So weit ich das sehe enthält die Struktur dirent::d_name den jeweiligen Namen.
Dann würde ich Dir empfehlen, die Namen in string's zu speichern. Das Array 'strplugfiles' ist lediglich ein Array von (einzelnen!) Zeichen (Typ char). Dies eignet sich dafür nicht!
Damit Du Dich nicht mit der 100 und dem Index 'ipcount' herumschlagen musst, nehme besser einen vector. Alles zusammen:#include <iostream> #include <dirent.h> #include <sstream> #include <string> #include <vector> using namespace std; int main(int argc, char *argv[]) { vector< string > plugfiles; // ein Speicher für strings //dir handle DIR *dirHandle; struct dirent * dirEntry; dirHandle = opendir("."); if (dirHandle) { while (0 != (dirEntry = readdir(dirHandle))) { if(strcmp(dirEntry->d_name, ".") == 0 || strcmp(dirEntry->d_name, "..") == 0) { continue; } stringstream strstrTemp1; strstrTemp1 << dirEntry->d_name; plugfiles.push_back( strstrTemp1.str() ); cout << plugfiles.back() << endl; // letzten Eintrag ausgeben } closedir(dirHandle); } return 0; }statt
stringstream strstrTemp1; strstrTemp1 << dirEntry->d_name; plugfiles.push_back( strstrTemp1.str() );geht wahrscheinlich auch
plugfiles.push_back( string( dirEntry->d_name ) );Gruß
Werner
-
Danke für Deine Antwort. Habs soeben ausprobiert und funktioniert hervorragend. Dass es in C++ Vektoren gibt habe ich noch nie gehört.