Hallo!
Gibt es eigentlich unter C++ Funktionen um ein Verzeichnis auszulesen oder muss ich da auf die C Funktionen zurückgreifen?
Danke schon im Vorraus!
Nicolas
Hallo!
Gibt es eigentlich unter C++ Funktionen um ein Verzeichnis auszulesen oder muss ich da auf die C Funktionen zurückgreifen?
Danke schon im Vorraus!
Nicolas
Da musst du die C-Funktionen nehmen.
c ya,
Tobias
He hab ich gerade bei mir im Projekte/ Ordner gefunden
Vielleicht findest du das ja nützlich.Code:/* Copyright (c) 2002 Tobias Kortkamp Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include <dirent.h> #include <iostream> #include <string> #include <vector> namespace std { class dir { public: dir(const char *dir) { directory = opendir(dir); } std::vector<std::string> getEntries() { struct dirent *dirEntry; std::vector<std::string> retVal; if(directory == 0) { return retVal; } while((dirEntry=readdir(directory))) { curDirEntry = dirEntry; if(selectDirEntry()) { std::string str(dirEntry->d_name); retVal.push_back(str); } } return retVal; } ~dir() { closedir(directory); } virtual bool selectDirEntry() { //Check wheter we want this particular directory entry. //all informations are in the struct curDirEntry return true; } protected: struct dirent* curDirEntry; DIR *directory; }; } int main(void) { std::dir dir("./"); std::vector<std::string> ret = dir.getEntries(); std::cout << ret[6] << std::endl; }
c ya,
Tobias
Danke für deine gute Antwort. Aber wäre es nicht einfacher, einfach eine Funktion zu bauen statt eine ganze Klasse? Ich benutze ja die Klasse nur ein einziges Mal zum Auslesen des Verzeichnisinhaltes.
Nicolas
Hab das glaub ich mal so gemacht, dass man mit selectDirEntry() bestimmt Einträge im Verzeichnis zulassen bzw. weglassen kann. Dafür hilft dann curDirEntry. Sonst müsste man das über Funktionspointer machen.
c ya,
Tobias
Ach so. Ich hab das jetzt als eigene Funktion, denn das mit dem Eintrag auswählen brauch ich ja eh nicht. Nur weiß ich halt nicht genau, ob es überhaupt sinnvoll ist, in C++ Funktionen zu verwenden.
Nicolas
Also kompieren kann ich das mit dem g++, aber in /tmp, wo viel drinnsteht, bekomme ich damit nur:Original geschrieben von tkortkamp
He hab ich gerade bei mir im Projekte/ Ordner gefunden
Vielleicht findest du das ja nützlich.Code:/* Copyright (c) 2002 Tobias Kortkamp Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include <dirent.h> #include <iostream> #include <string> #include <vector> namespace std { class dir { public: dir(const char *dir) { directory = opendir(dir); } std::vector<std::string> getEntries() { struct dirent *dirEntry; std::vector<std::string> retVal; if(directory == 0) { return retVal; } while((dirEntry=readdir(directory))) { curDirEntry = dirEntry; if(selectDirEntry()) { std::string str(dirEntry->d_name); retVal.push_back(str); } } return retVal; } ~dir() { closedir(directory); } virtual bool selectDirEntry() { //Check wheter we want this particular directory entry. //all informations are in the struct curDirEntry return true; } protected: struct dirent* curDirEntry; DIR *directory; }; } int main(void) { std::dir dir("./"); std::vector<std::string> ret = dir.getEntries(); std::cout << ret[6] << std::endl; }
c ya,
Tobias
targetMap_e
Ja ist klar, dass gibt nur den 7. Eintrag im Verzeichnis aus. (6+1)
Ersetzt du das jetzt mit:Code://ret ist ein std::vector, in dem stehen alle Verzeichniseinträge(inkl . und ..) std::vector<std::string> ret = dir.getEntries(); //Hier bekommen wir mit ret[6] das 7. Element aus dem std::vector std::cout << ret[6] << std::endl;
bekommst du alle Verzeichniseinträge.Code:for(int i=0; i < ret.size(); i++) { std::cout << ret[i] << std::endl; }
c ya,
Tobias
Klar, das mit dem 7. Eintrag hatte ich zu schnell quergelesen, danke für den Hinweis.Original geschrieben von tkortkamp
Ja ist klar, dass gibt nur den 7. Eintrag im Verzeichnis aus. (6+1)
Ersetzt du das jetzt mit:Code://ret ist ein std::vector, in dem stehen alle Verzeichniseinträge(inkl . und ..) std::vector<std::string> ret = dir.getEntries(); //Hier bekommen wir mit ret[6] das 7. Element aus dem std::vector std::cout << ret[6] << std::endl;
bekommst du alle Verzeichniseinträge.Code:for(int i=0; i < ret.size(); i++) { std::cout << ret[i] << std::endl; }
c ya,
Tobias
Alledings ist das ret.size() unbekannt: `ret' undeclared (first use this function).
Es soll wohl eine Funktion sein, die die Grösse des Vektors bestimmt, aber ret.size() wird vom g++ nicht akzeptiert.
Wieso kann der gcc das Programm nicht kompilieren? Der ist doch auch ein C++-Kompiler, oder?
Nö ist er nicht, g++ ist der C++-Compiler, gcc der C-Compiler.
Und bei mir mit g++ 2.95.4 kompiliert der Code einwandfrei...
c ya,
Tobias
Ok, aber was ist mit ret.size()
?
Was soll damit sein? Ich hab doch schon geschrieben, dass das bei mir einwandfrei funktioniert... Deinen Fehler kann ich nicht reproduzieren.
Welche g++ version hast du? (mit g++ --version bekommst du die Versionsnummer)
EDIT: Sieht die main-Funktion bei dir so aus?
c ya,Code:int main(void) { std::dir dir("./"); std::vector<std::string> ret = dir.getEntries(); for(int i=0; i < ret.size(); i++) { std::cout << ret[i] << std::endl; } }
Tobias
Ok, ich hatte auch die Zeile mit ret= auskommentiert, das war das Problem
Übrigens gibt der g++ einige warnings:
dirlist0.cpp:80: warning: `class dir' has virtual functions but non-virtual destructor
dirlist0.cpp: In function `int main()':
dirlist0.cpp:89: warning: comparison between signed and unsigned
Aha
Um die Warnungen wegzubekommen änder die Zeiel:
for(int i=0; i < ret.size(); i++) in for(unsigned int i=0; i < ret.size(); i++)
und die zeile ~dir() in virtual ~dir()
c ya,
Tobias
Aha, funktioniert, danke.
Ich verstehe nicht ganz, wieso Du überhaupt virtual reingeschrieben hast, aber es funktioniert ja.
Lesezeichen