<?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[Warum der Segmentation fault?]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich habe gerade ein Problem. Ich will eine Liste von Dateien in eine Liste speichern und diese Ausgeben. Doch ich bekomme leider nur ein Segmentation fault als Antwort.</p>
<p>Hier meine Main-Funktion:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;./Filelist.h&quot;

int main(int argc, char** argv)
{

  Filelist i;
  i.addDir(&quot;/home/[User]/CPP/&quot;);
  i.printFiles();

  return 0;
}
</code></pre>
<p>Hier mal die Funktionen der Klasse Filelist:</p>
<pre><code class="language-cpp">Filelist::Filelist()
{
}

// _____________________________________________________________________________

void Filelist::printFiles()
{
  for (size_t i = 0; i &lt; _filenameList.size(); i++)
  {
    std::cout &lt;&lt; _filenameList[i] &lt;&lt; &quot;\n&quot;;
  }
}

// _____________________________________________________________________________

void Filelist::addDir(std::string path)
{
  DIR *dirHandle;
  struct dirent *dirEntry;

  // Open a folder
  dirHandle = opendir(path.c_str());

  do
  {
    dirEntry = readdir(dirHandle);
    if (dirEntry)
    {
      _filenameList.push_back(dirEntry-&gt;d_name);
      if (dirEntry-&gt;d_type == DT_DIR)
      {
        addDir(path + &quot;/&quot; + dirEntry-&gt;d_name);
      }
    }
  } while (dirEntry);
  closedir(dirHandle);
}
</code></pre>
<p>Könnt ihr mir sagen, was da falsch ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/291593/warum-der-segmentation-fault</link><generator>RSS for Node</generator><lastBuildDate>Mon, 17 Aug 2026 15:53:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/291593.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 22 Aug 2011 12:49:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Warum der Segmentation fault? on Mon, 22 Aug 2011 12:49:31 GMT]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich habe gerade ein Problem. Ich will eine Liste von Dateien in eine Liste speichern und diese Ausgeben. Doch ich bekomme leider nur ein Segmentation fault als Antwort.</p>
<p>Hier meine Main-Funktion:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;./Filelist.h&quot;

int main(int argc, char** argv)
{

  Filelist i;
  i.addDir(&quot;/home/[User]/CPP/&quot;);
  i.printFiles();

  return 0;
}
</code></pre>
<p>Hier mal die Funktionen der Klasse Filelist:</p>
<pre><code class="language-cpp">Filelist::Filelist()
{
}

// _____________________________________________________________________________

void Filelist::printFiles()
{
  for (size_t i = 0; i &lt; _filenameList.size(); i++)
  {
    std::cout &lt;&lt; _filenameList[i] &lt;&lt; &quot;\n&quot;;
  }
}

// _____________________________________________________________________________

void Filelist::addDir(std::string path)
{
  DIR *dirHandle;
  struct dirent *dirEntry;

  // Open a folder
  dirHandle = opendir(path.c_str());

  do
  {
    dirEntry = readdir(dirHandle);
    if (dirEntry)
    {
      _filenameList.push_back(dirEntry-&gt;d_name);
      if (dirEntry-&gt;d_type == DT_DIR)
      {
        addDir(path + &quot;/&quot; + dirEntry-&gt;d_name);
      }
    }
  } while (dirEntry);
  closedir(dirHandle);
}
</code></pre>
<p>Könnt ihr mir sagen, was da falsch ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2109109</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2109109</guid><dc:creator><![CDATA[bluepeople12]]></dc:creator><pubDate>Mon, 22 Aug 2011 12:49:31 GMT</pubDate></item><item><title><![CDATA[Reply to Warum der Segmentation fault? on Mon, 22 Aug 2011 12:53:13 GMT]]></title><description><![CDATA[<p>Nicht mit dem gegebenen Code. Aber Dein Debugger ist Dein Freund.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2109112</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2109112</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Mon, 22 Aug 2011 12:53:13 GMT</pubDate></item><item><title><![CDATA[Reply to Warum der Segmentation fault? on Mon, 22 Aug 2011 13:54:20 GMT]]></title><description><![CDATA[<p>Kann mir gut vorstellen, dass Funktionen wie <code>opendir</code> und <code>readdir</code> im Fehlerfall was zurückgeben, womit man nicht weiterarbeiten sollte. Am besten guckst du mal in die Doku der API und prüfst die Rückgabewerte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2109151</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2109151</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Mon, 22 Aug 2011 13:54:20 GMT</pubDate></item><item><title><![CDATA[Reply to Warum der Segmentation fault? on Mon, 22 Aug 2011 16:26:39 GMT]]></title><description><![CDATA[<blockquote>
<p>RETURN VALUE<br />
The opendir() and fdopendir() functions return a pointer to the directory stream. On error, NULL is returned, and errno is set appropriately.</p>
</blockquote>
<p>Dh, wenn der Ordner nicht existiert oä., gibts NULL zurück und du dereferenzierst es.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2109216</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2109216</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Mon, 22 Aug 2011 16:26:39 GMT</pubDate></item><item><title><![CDATA[Reply to Warum der Segmentation fault? on Mon, 22 Aug 2011 16:30:39 GMT]]></title><description><![CDATA[<blockquote>
<pre><code>dirHandle = opendir(path.c_str());
</code></pre>
</blockquote>
<p>Ich vermute, dass diese Funktion NULL zurückgibt. Wenn das der Fall ist dann bin ich mir sicher, dass dein Programm hier crasht:</p>
<pre><code>dirEntry = readdir(dirHandle);
</code></pre>
<p>Fazit: es wäre gut nach dem opendir-Aufruf zu überprüfen ob dirHandle NULL ist oder nicht.</p>
<p>Edit:<br />
Upps, Ethon war schneller <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2109217</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2109217</guid><dc:creator><![CDATA[c0ff33.alex]]></dc:creator><pubDate>Mon, 22 Aug 2011 16:30:39 GMT</pubDate></item><item><title><![CDATA[Reply to Warum der Segmentation fault? on Mon, 22 Aug 2011 19:54:13 GMT]]></title><description><![CDATA[<p>Also ich hab ein wenig mit strace rumprobiert und hab nun folgende Funktion mittlerweile. Diese funktioniert auch soweit ganz gut.</p>
<pre><code class="language-cpp">void Filelist::addDir(std::string path)
{
  DIR *dirHandle;
  struct dirent *dirEntry;

  // Open a folder
  dirHandle = opendir(path.c_str());

  do
  {
    dirEntry = readdir(dirHandle);
    if (dirEntry)
    {
      std::string s;
      s.append(path);
      s.append(dirEntry-&gt;d_name);
      _filenameList.push_back(s.c_str());

      // add the subfolder, if the actual file is a folder
      if (isdir(s))
      {
        std::cout &lt;&lt; &quot;--- &quot; &lt;&lt; s &lt;&lt; &quot; is a dir.\n&quot;;
        // if the dir is . or .., then don't search these folders
        // but all other folder
        if (!((s[s.size() - 1] == '.') || 
          ((s[s.size() - 1] == '.') &amp;&amp; (s[s.size() - 2] == '.'))))
        {
          s.append(&quot;/&quot;);
          addDir(s);
        }
      }

    }
  } while (dirEntry);
  closedir(dirHandle);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2109308</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2109308</guid><dc:creator><![CDATA[bluepeople12]]></dc:creator><pubDate>Mon, 22 Aug 2011 19:54:13 GMT</pubDate></item></channel></rss>