<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Ordnerstruktur einlesen]]></title><description><![CDATA[<p>Hallo,</p>
<p>wie könnte ich eine komplette Ordnerhierarchie mit einem c++ Programm einlesen? Ich habe dabei keine Boardmittel zur Hand da ich ein externes Gerät einlesen möchte.</p>
<p>Meine einzige Möglichkeit besteht darin, mir ein Directory Listing mit ls bzw ls-l zu holen um zwischen Ordnern und Dateien unterscheiden zu können. Für die obere Verzeichnis Ebene war das ganze relativ simpel. Ich bekomme aber irgendwie die Rekursion nicht auf die Kette.</p>
<p>Jemand einen Tip?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/273895/ordnerstruktur-einlesen</link><generator>RSS for Node</generator><lastBuildDate>Thu, 27 Aug 2026 21:45:19 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/273895.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 15 Sep 2010 07:45:10 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Ordnerstruktur einlesen on Wed, 15 Sep 2010 07:45:10 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>wie könnte ich eine komplette Ordnerhierarchie mit einem c++ Programm einlesen? Ich habe dabei keine Boardmittel zur Hand da ich ein externes Gerät einlesen möchte.</p>
<p>Meine einzige Möglichkeit besteht darin, mir ein Directory Listing mit ls bzw ls-l zu holen um zwischen Ordnern und Dateien unterscheiden zu können. Für die obere Verzeichnis Ebene war das ganze relativ simpel. Ich bekomme aber irgendwie die Rekursion nicht auf die Kette.</p>
<p>Jemand einen Tip?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1952798</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1952798</guid><dc:creator><![CDATA[cardex]]></dc:creator><pubDate>Wed, 15 Sep 2010 07:45:10 GMT</pubDate></item><item><title><![CDATA[Reply to Ordnerstruktur einlesen on Wed, 15 Sep 2010 09:27:23 GMT]]></title><description><![CDATA[<p>Also wenn du die Files hast, dann musst du nur schauen, ob das &quot;File&quot; ein Ordner ist und dann rekursiv weiterfahren.</p>
<p>Wenn es um die Schnittstelle geht auf das Filesystem, dann nimmst du da entweder Betriebssystemspezifische Sachen, wie aus der WinAPI, oder aber du benutzt <a href="http://www.boost.org/doc/libs/1_43_0/libs/filesystem/doc/index.htm" rel="nofollow">boost::filesystem</a>. Dort hat es auch Beispiele dabei, mit denen das mit der Rekursion klar werden sollte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1952842</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1952842</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Wed, 15 Sep 2010 09:27:23 GMT</pubDate></item><item><title><![CDATA[Reply to Ordnerstruktur einlesen on Wed, 15 Sep 2010 09:41:37 GMT]]></title><description><![CDATA[<p>Erstmal danke für die Hilfe, leider bringt mich das so nicht weiter. Fremde Bibliotheken bringen mir nichts da es sich bei dem externen Gerät um ein Telefon handelt, welches über ein separates Programm angesprochen wird und nicht als Datenträger im OS auftaucht.</p>
<p>Ich kann dem Gerät wie gesagt nur über eine externe Programmschnittstelle ein Directory Listing entlocken. Das mache ich bisher für die Root Ebene auch erfolgreich.</p>
<p>Sprich ich lese ein normales &quot;ls&quot; und ein &quot;ls -l&quot; in 2 Arrays und vergleiche anschließend die Arrays Schritt für Schritt miteinander. Beginnt das Array mit den &quot;ls -l&quot; Ausgaben mit einem d ist es ein Ordner (in meinem Fall wichtig um passende Icons für ein TreeWidget anzuhängen) ansonsten halt eine Datei.</p>
<p>Das in deinem Post im Nebensatz behandelte &quot;..und dann rekursiv weiterfahren&quot; bekomme ich halt nicht auf die Kette.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1952848</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1952848</guid><dc:creator><![CDATA[cardex]]></dc:creator><pubDate>Wed, 15 Sep 2010 09:41:37 GMT</pubDate></item><item><title><![CDATA[Reply to Ordnerstruktur einlesen on Wed, 15 Sep 2010 09:52:59 GMT]]></title><description><![CDATA[<p>cardex schrieb:</p>
<blockquote>
<p>Das in deinem Post im Nebensatz behandelte &quot;..und dann rekursiv weiterfahren&quot; bekomme ich halt nicht auf die Kette.</p>
</blockquote>
<p>Wenn du die Verzeichnisse im root Verzeichnis kennst, dann kannst du auf diesen wiederum deinen Verzeichnisholbefehl ausführen, usw.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1952850</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1952850</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 15 Sep 2010 09:52:59 GMT</pubDate></item><item><title><![CDATA[Reply to Ordnerstruktur einlesen on Wed, 15 Sep 2010 10:33:42 GMT]]></title><description><![CDATA[<p>Ich steh da wohl wirklich auf dem Schlauch. Hier mal Teile des Codes für die Root Ebene.</p>
<pre><code class="language-cpp">void readDeviceThread::processLogFile() {
    int i = 1;
    //schnipp
    //hier werden die 2 Listing in einem QProzess nacheinander in die Dateien
    //list.log und list2.log geschrieben.
    //schnapp

    QFile log1(&quot;list.log&quot;);
    if (!log1.open(QIODevice::ReadOnly | QIODevice::Text)) {
             return;
    }
    QFile log2(&quot;list2.log&quot;);
    if (!log2.open(QIODevice::ReadOnly | QIODevice::Text)) {
             return;
    }
    QTextStream readLog1(&amp;log1);
    QTextStream readLog2(&amp;log2);
    while (!readLog1.atEnd()) {
        temp1[i] = readLog1.readLine();
        temp2[i] = readLog2.readLine();
        i++;
    }
    dirCount = i;
    log1.close();
    log2.close();
    createItems(dirCount);
}

void readDeviceThread::createItems(int dirCount) {
    QIcon folder;
    QString ls[200] = temp1;
    QString lsl[200] = temp2;
    for(int i=1;i&lt;dirCount;i++) {
        if(lsl[i].startsWith(&quot;d&quot;)) {
            folder.addFile(&quot;folder.png&quot;);
            QTreeWidgetItem *Children = new QTreeWidgetItem(pItems);
            Children-&gt;setIcon(0, folder);
            Children-&gt;setText(0, ls[i]);
        }
        else {
            folder.addFile(&quot;file.png&quot;);
            QTreeWidgetItem *Children = new QTreeWidgetItem(pItems);
            Children-&gt;setIcon(0, folder);
            Children-&gt;setText(0, ls[i]);
        }
    }
    emit sendFinish(3);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1952871</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1952871</guid><dc:creator><![CDATA[cardex]]></dc:creator><pubDate>Wed, 15 Sep 2010 10:33:42 GMT</pubDate></item><item><title><![CDATA[Reply to Ordnerstruktur einlesen on Wed, 15 Sep 2010 12:39:06 GMT]]></title><description><![CDATA[<p>wie sag ichs nett :)))</p>
<p>Logikfehler ... falls ls-l nur Directories liefert ist dessen Länge nicht unbedingt == ls ... falls ls -l nur ein d davorsetzt, wofür dann 2 Listen?<br />
Dann fliegt dir das um die Ohren:</p>
<pre><code class="language-cpp">while (!readLog1.atEnd()) { // &lt;&lt; können verschieden lang sein
        temp1[i] = readLog1.readLine();
        temp2[i] = readLog2.readLine();
        i++;
    }
</code></pre>
<p>... was passiert bei ls / ls -l bei folgender Struktur?</p>
<pre><code>dir1_das_ist_ein_dir
dir2_das_ist_noch_ein_dir
datei1_das_ist_eine_datei_namens_datei1
datei2_das_ist_eine_datei_namens_datei2
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1952925</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1952925</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Wed, 15 Sep 2010 12:39:06 GMT</pubDate></item><item><title><![CDATA[Reply to Ordnerstruktur einlesen on Wed, 15 Sep 2010 19:13:28 GMT]]></title><description><![CDATA[<p>bessere Infos, bessere Antowort ... siehe unten</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1952927</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1952927</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Wed, 15 Sep 2010 19:13:28 GMT</pubDate></item><item><title><![CDATA[Reply to Ordnerstruktur einlesen on Wed, 15 Sep 2010 13:27:47 GMT]]></title><description><![CDATA[<p>Nur mit C++ ist <strong>dirent.h</strong> ,<strong>sys/types.h</strong> und <strong>sys/stat.h</strong> was du brauchst.<br />
Benutz immer dirent.h um Dateien aus Ordnern auszulesen ist eigendlich recht einfach aber eine Ordnerhierarchie das würde auch gehn.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1952948</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1952948</guid><dc:creator><![CDATA[CCodex]]></dc:creator><pubDate>Wed, 15 Sep 2010 13:27:47 GMT</pubDate></item><item><title><![CDATA[Reply to Ordnerstruktur einlesen on Wed, 15 Sep 2010 15:37:52 GMT]]></title><description><![CDATA[<p>padreigh schrieb:</p>
<blockquote>
<p>wie sag ichs nett :)))</p>
<p>Logikfehler ... falls ls-l nur Directories liefert ist dessen Länge nicht unbedingt == ls ... falls ls -l nur ein d davorsetzt, wofür dann 2 Listen?<br />
Dann fliegt dir das um die Ohren:</p>
<pre><code class="language-cpp">while (!readLog1.atEnd()) { // &lt;&lt; können verschieden lang sein
        temp1[i] = readLog1.readLine();
        temp2[i] = readLog2.readLine();
        i++;
    }
</code></pre>
</blockquote>
<p>nee der teil ist schon in ordnung. in beiden logfiles steht immer die identische anzahl an einträgen. nur einmal nur die datei bzw ordnernamen und einmal das linux typische ausführlichere listing. und da ich mir das rausschneiden der namen aus dem ausführlichen listing sparen will gibt es halt 2 files.</p>
<p>die files sehen dann zB so aus.</p>
<pre><code>sbin
proc
init.rc
init.goldfish.rc
init
default.prop
data
root
dev
</code></pre>
<pre><code>drwxr-x--- root     root              1970-01-01 00:00 sbin
dr-xr-xr-x root     root              1970-01-01 00:00 proc
-rwxr-x--- root     root        12215 1970-01-01 00:00 init.rc
-rwxr-x--- root     root         1677 1970-01-01 00:00 init.goldfish.rc
-rwxr-x--- root     root       103112 1970-01-01 00:00 init
-rw-r--r-- root     root          118 1970-01-01 00:00 default.prop
drwxrwx--x system   system            2010-09-12 11:25 data
drwx------ root     root              2010-01-28 00:59 root
drwxr-xr-x root     root              2010-09-15 07:41 dev
</code></pre>
<p>deinen beispielcode muss ich erst noch verdauen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1953029</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1953029</guid><dc:creator><![CDATA[cardex]]></dc:creator><pubDate>Wed, 15 Sep 2010 15:37:52 GMT</pubDate></item><item><title><![CDATA[Reply to Ordnerstruktur einlesen on Thu, 16 Sep 2010 07:27:50 GMT]]></title><description><![CDATA[<p>In dem alten sind Fehler drin, versuch das hier zu verstehen oder machs selbst <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<pre><code>$ cat ressorces.qrc 
&lt;RCC&gt;
    &lt;qresource prefix=&quot;/icon&quot;&gt;
        &lt;file alias=&quot;file.png&quot;&gt;image.png&lt;/file&gt;
        &lt;file alias=&quot;folder.png&quot;&gt;folder.png&lt;/file&gt;
    &lt;/qresource&gt;
&lt;/RCC&gt;

$ cat tessst.pro
SOURCES += main.cpp
RESOURCES += ressorces.qrc
</code></pre>
<pre><code class="language-cpp">$ cat main.cpp
#include &lt;QApplication&gt;

#include &lt;Qt&gt;
#include &lt;QStringList&gt;
#include &lt;QDebug&gt;
#include &lt;QTreeWidget&gt;
#include &lt;QTableWidgetItem&gt;

/// fake return in desired format
QStringList createLog(QString path)
{
    QStringList listOfFiles;                                                                      

    // Simulated Structure:  
    // .
    // |-- data
    // |   |-- leeresVerz
    // |   `-- data-datei1
    // |-- default.prop
    // |-- init
    // |-- init.goldfish.rc
    // |-- init.rc
    // |-- root
    // |   `-- root-datei1
    // `-- sbin
    //     `-- sbin-datei1

    // ///////////////////////////////////////////////////////////////////////////////////////////////
    // gefakte returns ;)                                                                           //
    // ///////////////////////////////////////////////////////////////////////////////////////////////
    if (path==&quot;/&quot;)                                                                                  //
    {                                                                                               //
        listOfFiles &lt;&lt; &quot;drwxr-x--- root     root              1970-01-01 00:00 sbin&quot;                //
                    &lt;&lt; &quot;-rwxr-x--- root     root        12215 1970-01-01 00:00 init.rc&quot;             //
                    &lt;&lt; &quot;-rw-r--r-- root     root          118 1970-01-01 00:00 default.prop&quot;        //
                    &lt;&lt; &quot;drwxrwx--x system   system            2010-09-12 11:25 data&quot;                //
                    &lt;&lt; &quot;-rwxr-x--- root     root         1677 1970-01-01 00:00 init.goldfish.rc&quot;    //
                    &lt;&lt; &quot;-rwxr-x--- root     root       103112 1970-01-01 00:00 init&quot;                //
                    &lt;&lt; &quot;drwx------ root     root              2010-01-28 00:59 root&quot;;               //
    }                                                                                               //
    // ///////////////////////////////////////////////////////////////////////////////////////////////
    else if (path == &quot;sbin&quot;)                                                                        //
    {                                                                                               //
        listOfFiles &lt;&lt; &quot;-rw-r--r-- root     root          118 1970-01-01 00:00 sbin-datei1&quot;;        //
    }                                                                                               //
    // ///////////////////////////////////////////////////////////////////////////////////////////////
    else if (path ==&quot;data&quot;)                                                                         //
    {                                                                                               //
        listOfFiles &lt;&lt; &quot;-rw-r--r-- root     root          118 1970-01-01 00:00 data-datei1&quot;         //
                    &lt;&lt; &quot;drwxr-x--- root     root              1970-01-01 00:00 leeresVerz&quot;;         //
    }                                                                                               //
    // ///////////////////////////////////////////////////////////////////////////////////////////////
    else if (path==&quot;root&quot;)                                                                          //
    {                                                                                               //
        listOfFiles &lt;&lt; &quot;-rw-r--r-- root     root          118 1970-01-01 00:00 root-datei1&quot;;        //
    }                                                                                               //
    // ///////////////////////////////////////////////////////////////////////////////////////////////
    else if (path==&quot;leeresVerz&quot;)                                                                    //
    {                                                                                               //
        return QStringList();                                                                       //
    }                                                                                               //
    // ///////////////////////////////////////////////////////////////////////////////////////////////

    qDebug() &lt;&lt; &quot;returned directory content for path: '&quot; + path + &quot;'&quot;;
    qDebug() &lt;&lt; listOfFiles.join(&quot;\n&quot;);

    QStringList parsedList;

    const QString permissions (&quot;((r|w|x|-){3}){3}\\s+&quot;);                     // (((r oder w oder x oder -) DREIMAL) DREIMAL) whitespaces MEHRALS0
    const QString owner_group (&quot;(\\w+\\s+){2}&quot;);                             // (zahlen/buchstaben MEHRALS0, whitespaces MEHRALS0) ZWEIMAL
    const QString filesize    (&quot;\\d+\\s+&quot;);                                  // (zahlen MEHRALS0 whitespaces MEHRALS0
    const QString date_time   (&quot;\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}\\s&quot;);   // zahlen VIER - zahlen ZWEI - zahlen ZWEI usw.

    const QRegExp files (&quot;^-&quot;+permissions+owner_group+filesize+date_time);   // stringstart mit - dann rest
    const QRegExp dirs  (&quot;^d&quot;+permissions+owner_group+date_time);            // stringstart mit d dann rest

     foreach (QString one, listOfFiles)
    {
        bool isDir = one.startsWith(&quot;d&quot;);

        QString test(one);

        if (isDir)
            test.replace(dirs,&quot;&quot;).prepend(&quot;_DIR_&quot;);   // remove all the stuff beside file name
        else                                          // you could get problems if filenames allow ' ' in the name and use '\'  
            test.replace(files,&quot;&quot;).prepend(&quot;_FIL_&quot;);  // or smth like &quot;&lt;whole name&gt;&quot; then. best check and adapt it accordingly 

        if (test==one) 
        {
            qDebug() &lt;&lt; &quot;\tPARSING WENT WRONG: &quot; &lt;&lt; one;
            if (test.startsWith(d))
                parsedList &lt;&lt; &quot;_!!!_DIR_&quot;;
            else
                parsedList &lt;&lt; &quot;_!!!_FIL_&quot;;
        } 
        else           
        { 
            parsedList &lt;&lt; test; 
        }
    }

    qDebug() &lt;&lt; &quot;parsed directory: '&quot; + path + &quot;'&quot;;
    qDebug() &lt;&lt; parsedList.join(&quot;\n&quot;);

    return parsedList;
}

QTreeWidgetItem * createItems(const QString path = &quot;/&quot;, QTreeWidgetItem * parent=0) {

    const QIcon folder(&quot;:icon/folder.png&quot;);  // 2 famfamsilkicons v.0013 google it if you want :)
    const QIcon file(&quot;:icon/file.png&quot;);

    QStringList filesAndDirs;

    if (!parent)
    {
        // falls diese Methode mit createItems() aufgerufen wurde, konstruiere das &quot;root&quot; QTreeWidgetItems
        parent = new QTreeWidgetItem();
        parent-&gt;setIcon(0,folder);
        parent-&gt;setText(0,path);
    }

    // hole dir die Struktur des root-dirs
    filesAndDirs = createLog(path);

    QTreeWidgetItem *child;

    // we added a directory but it has no content
    if ( filesAndDirs.isEmpty() )
    {
        child = new QTreeWidgetItem(parent);
        child-&gt;setData(0, Qt::DisplayRole, &quot;&lt;empty subdir&gt;&quot;);
        // nix rekursion, aber vielleicht ein empty icon?
    }

    // für jede Zeile (präfixed mit _FIL_ oder _DIR_)
    foreach (QString one, filesAndDirs)
    {
        if (one.startsWith(&quot;_!!!_FIL_&quot;))
        {
            child = new QTreeWidgetItem(parent);
            // append some kind of &quot;broken file&quot; image for this ...
            // append some kind of &quot;parsing broken&quot; text 
            // nix rekursion
        }
        if (one.startsWith(&quot;_!!!_DIR_&quot;))
        {
            child = new QTreeWidgetItem(parent);
            // append some kind of &quot;broken folder&quot; image for this ...
            // append some kind of &quot;parsing broken&quot; text 
            // nix rekursion da wir nicht wissen worein wir rekursieren sollen
        }
        if (one.startsWith(&quot;_DIR_&quot;))
        {
            child = new QTreeWidgetItem(parent); // hängs unters parent, setze icon und text ohne präfix
            child-&gt;setData(0, Qt::DecorationRole, folder);
            child-&gt;setData(0, Qt::DisplayRole, one.remove(0,5) );

            createItems(one, child); // rekursion .. nun muss natürlich der Inhalt des Folders unter dieses child gehängt werden 

            // der returnwert ist egal da die items direkt eingehängt werden
        } else {
            child = new QTreeWidgetItem(parent);  // hängs unters parent, setze icon und text ohne präfix
            child-&gt;setData(0, Qt::DecorationRole, file);
            child-&gt;setData(0, Qt::DisplayRole, one.remove(0,5) );
            // nix rekursion
        }
    }

    return parent;
}

int main(int argv, char *args[])
{

    QApplication app(argv, args);

    QTreeWidget t;
    t.setColumnCount(1);
    t.addTopLevelItem(   createItems() );
    t.expandAll();
    t.resizeColumnToContents(1);
    t.show();

    return app.exec();
}
</code></pre>
<p>edit: 2 ideen für broken parsing</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1953138</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1953138</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Thu, 16 Sep 2010 07:27:50 GMT</pubDate></item><item><title><![CDATA[Reply to Ordnerstruktur einlesen on Wed, 15 Sep 2010 20:18:28 GMT]]></title><description><![CDATA[<p>wow vielen dank <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1953163</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1953163</guid><dc:creator><![CDATA[cardex]]></dc:creator><pubDate>Wed, 15 Sep 2010 20:18:28 GMT</pubDate></item><item><title><![CDATA[Reply to Ordnerstruktur einlesen on Wed, 15 Sep 2010 20:45:53 GMT]]></title><description><![CDATA[<p>hab mal noch mehr kommentiert ... wenn du fragen hast <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> nur zu</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1953175</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1953175</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Wed, 15 Sep 2010 20:45:53 GMT</pubDate></item></channel></rss>