Ausgabe mit opendir und readdir - Probleme bei Verzeichnissen



  • Hi,

    ich programmiere mir gerade unter Knoppix ein C++ Progamm, welches mir den Inhalt eines Verzeichnissen, samt Dateien und Ordner mit Informationen anzeigen soll.

    Mein schaut in ungefähr so aus:

    DIR *dir;
    dir = opendir(".");
    struct dirent *ent;
    while (ent = readdir(dir))
    {
       if (opendir(ent->d_name) == NULL)
       {
          cout << ent->d_name << endl;
       }
       else
       {
          // hier muss es sich um ein verzeichnis handeln
          cout << ent->d_name << endl;
       }
    }
    

    Ich habe den Code von Hand so hingeschriebe, wie ich Ihn in Erinnerung habe, da ich grad keinen Zugang habe.

    Die Dateinamen werden bei mir richtig angezeigt. Allerdings werden bei den ersten drei Einträge nur Hyroglyphen. Zwei der drei Einträge könnten ja dieses . und .. sein. Dies könnte die Hyroglyphen erklären.

    Wenn ich nun in dem else Part einfach nur ein "Hallo" ausgebe schaut meine Ausgabe soaus, dass der erste Eintrag wieder Hyroglyphen sind. Danach folgen dann zwei mal Hallo und danach erst die richtigen Dateien.

    Kann jemand diesem Problem Folgen und mir helfen? Ich möchte einfach dass alle Verzeichnisse und Dateien richtig angezeigt werden.

    Stefan



  • Hi!

    Bei mir klappt ähnliche Funktion wunderbar.
    Wenn du möchtest kann ich dir ein wenig Code posten



  • Hi,

    das wäre nett von dir, wenn du etwas code posten könntest.

    Stefan



  • Hi!

    Ok, das ganze is ne Klasse

    readdir.hpp

    #ifndef READDIR_HPP
    #define READDIR_HPP
    
    #include <string>
    #include <vector>
    #include <set>
    
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>
    
    using namespace std;
    
    // reads a given directory and returns the filenames in a std::vector
    class cReaddir
    {
    public :
    
            //
            struct sFile
            {
                    string _file;
                    string _path;
                    int    _size; // only applies to files, in bytes (like ls -l)
    
                    sFile () {_size=0;}
            };
    
            // read all files from "dir" into "out", set "rec" to true to search recursively
            bool readDir (vector<sFile>& out, string dir, bool rec=false);
    };
    
    #endif // READDIR_HPP
    

    readdir.cpp

    #include "readdir.hpp"
    #include "iostream"
    
    bool cReaddir::readDir (vector<cReaddir::sFile>& out, string dir, bool rec)
    {
            struct stat    fileInfo;        // information about the file / directory
            struct dirent *curFile;         // current file we check in the loop
            DIR    *directory;              // handle to directory
            string  path;                   // path+filename of a torrent
            sFile  file;                    // saved in vector
    
            // add needed /
            if (dir[dir.size()-1] != '/')
                    dir += "/";
    
            //
            if ((directory = opendir (dir.c_str())) == NULL)
                    return false;
    
            // fails on last file
            while ( (curFile = readdir (directory)) != NULL)
            {
                    // not "." or ".."
                    if (strcmp (curFile->d_name, "." ) && 
                        strcmp (curFile->d_name, ".." ))
                    {
                            path = dir;
                            path += curFile->d_name;
    
                            // get file info
                            if (stat(path.c_str(), &fileInfo ) == -1)
                            {
    						   continue;
                            }
    
                            // check type
                            if (S_ISREG (fileInfo.st_mode)) // regular file ?
                            {
                                    file._file = curFile->d_name;
                                    file._path = dir;
                                    file._size = fileInfo.st_size;
                                    out.push_back (file);
                            }
                            else if (S_ISDIR(fileInfo.st_mode) && rec) // directory ?
                            {
                                    // save dir including /, to make it recognisable as a directory
                                    file._file = curFile->d_name; file._file += "/";
                                    file._path = dir;
                                    out.push_back (file);
    
                                    // search in dir
                                    readDir (out, file._path+file._file, true);
                            }
                    } // if not "..", "."
            } //end while
    
            closedir (directory);
    
            return true;    
    }
    

    Edit:
    Bugfix


Anmelden zum Antworten