<?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[Dateien&#x2F;Verzeichnisse auflisten (Unicode)]]></title><description><![CDATA[<p>Hi,</p>
<p>ich versuche gerade ein Programm zu schreiben, das alle Dateien und Unterordner in einem Ordner in eine html datei schreibt und auch mit Unicode zurecht kommt.</p>
<p>Allerdings hab ich so ein paar Probleme:</p>
<p>Ich benutze WinXP 32bit und GCC (MinGW) 3.4.2 und NTFS, daher die #defines</p>
<p>1. Die Funktion &quot;swprintf&quot; wird in meiner Headerdatei so &quot;int swprintf (wchar_t *s, const wchar_t *format, ...);&quot;<br />
und auf vielen Internetseiten so &quot;int swprintf(wchar_t *s, size_t n, const wchar_t *format, ...);&quot; deklariert.<br />
2. Bekomme ich beim 2. swprintf Aufruf (in main) einen Bufferoverflow bzw. einen SIGSEGV<br />
3. Wenn ich das size_t weglasse, bekomme ich dafür beim fputws() in writeHeaderW() einen bufferoverflow<br />
3. Wenn man das ganze mit Visual Studio kompiliert, läuft das ganze zwar, aber nach ein paar dateien bekommt man einen &quot;Stackoverflow&quot;<br />
EDIT: Das Problem wurde jetzt (glaub ich) gelöst, indem ich den Speicher &quot;mallociere&quot;</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;
#include &lt;time.h&gt;

#define MAX_PATH_SIZE 32767 + 1
#define MAX_FILE_NAME 255 + 1

wchar_t ed2k;
FILE *f;

int parse_directory(wchar_t *directory)
{
    wchar_t *currentDirectory = (wchar_t *)malloc(MAX_PATH_SIZE * sizeof(wchar_t));
    wchar_t *nextDirectory = (wchar_t *)malloc(MAX_PATH_SIZE * sizeof(wchar_t));
    HANDLE hFind = INVALID_HANDLE_VALUE;
    WIN32_FIND_DATAW lpFindFileData;

    fwprintf(f, L&quot;Current directory: %s\n&quot;, directory);

    swprintf(currentDirectory, MAX_PATH_SIZE, L&quot;%s\\*&quot;, directory);

    hFind = FindFirstFileW(currentDirectory, &amp;lpFindFileData);

    if (hFind == INVALID_HANDLE_VALUE)
    {
        fwprintf(f, L&quot;Invalid file handle. Error#: %lu.\n&quot;, GetLastError());
        free(currentDirectory);
        free(nextDirectory);
        return 1;
    }

    do
    {
        if (!(lpFindFileData.cFileName[0] == L'.'
            &amp;&amp; ((lpFindFileData.cFileName[1] == L'.'
            &amp;&amp; lpFindFileData.cFileName[2] == L'\0')
            || lpFindFileData.cFileName[1] == L'\0')))
        {
            if (lpFindFileData.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY)
            {
                fwprintf(f, L&quot;Dir: %s\n&quot;, lpFindFileData.cFileName);
                swprintf(nextDirectory, MAX_PATH_SIZE, L&quot;%s\\%s&quot;,
                directory, lpFindFileData.cFileName);
                parse_directory(nextDirectory);
            }
            else
            {
                fwprintf(f, L&quot;File: %s\n&quot;, lpFindFileData.cFileName);
            }
        }
    }
    while (FindNextFileW(hFind, &amp;lpFindFileData) != 0);

    free(currentDirectory);
    free(nextDirectory);

    FindClose(hFind);

    if (GetLastError() != ERROR_NO_MORE_FILES)
    {
        fwprintf(f, L&quot;FindNextFile error. Error is %lu.\n&quot;, GetLastError());
        return 1;
    }

    return 0;
}

void writeHeaderW(wchar_t *directory, wchar_t *cLocalTime)
{
    fputws(L&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;utf-8\&quot;?&gt;\n&quot;
           L&quot;&lt;!DOCTYPE html PUBLIC \&quot;-//W3C//DTD XHTML 1.1//EN\&quot; &quot;
           L&quot;\&quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\&quot;&gt;\n&quot;
           L&quot;&lt;html&gt;\n&quot;
           L&quot;&lt;head&gt;\n&quot;, f);

    fwprintf(f, L&quot;&lt;title&gt;Contents of: \&quot;%s\&quot; (\&quot;%s\&quot;)&lt;/title&gt;\n&quot;
                L&quot;&lt;link rel=\&quot;stylesheet\&quot; href=\&quot;style.css\&quot; type=\&quot;text/css\&quot;&quot;
                L&quot; title=\&quot;Default-Style\&quot; media=\&quot;screen\&quot; /&gt;\n&quot;
                L&quot;&lt;/head&gt;\n&quot;
                L&quot;&lt;body&gt;\n&quot;, directory, cLocalTime);

    fwprintf(f, L&quot;&lt;h1&gt;Contents of: \&quot;%s\&quot; (\&quot;%s\&quot;)&lt;/h1&gt;\n&quot;
                L&quot;&lt;table&gt;\n&quot;
                L&quot;  &lt;tr&gt;\n&quot;
                L&quot;    &lt;th&gt;Name&lt;/th&gt;\n&quot;
                L&quot;    &lt;th&gt;Filesize&lt;/th&gt;\n&quot;
                L&quot;    &lt;th&gt;InodeChangeTime&lt;/th&gt;\n&quot;
                L&quot;    &lt;th&gt;ModificationTime&lt;/th&gt;\n&quot;
                L&quot;    &lt;th&gt;AccessTime&lt;/th&gt;\n&quot;, directory, cLocalTime);

    if (ed2k == L'1')
    {
        fputws(L&quot;    &lt;th&gt;ed2k-Hash&lt;/th&gt;\n&quot;, f);
    }
    fputws(L&quot;  &lt;/tr&gt;\n&quot;, f);
}

void writeFooterW()
{
    fputws(L&quot;&lt;/table&gt;\n&quot;
           L&quot;&lt;/body&gt;\n&quot;
           L&quot;&lt;/html&gt;&quot;, f);
}

int main(int argc, char **argv)
{
    if (argc != 3)
    {
        fprintf(stdout, &quot;usage: %s [directory] [ed2k 0/1]\n&quot;, argv[0]);
        return 1;
    }

    wchar_t *directory = (wchar_t *) malloc(MAX_PATH_SIZE * sizeof(wchar_t));
    wchar_t filename[MAX_FILE_NAME], cLocalTime[20];
    swprintf(directory, MAX_PATH_SIZE, L&quot;\\\\?\\%s&quot;, L&quot;C:&quot;); // argv[1]
    ed2k = L'0'; // argv[2]
    time_t tTime;
    time(&amp;tTime);
    struct tm *tmLocalTime = localtime(&amp;tTime);
    wcsftime(cLocalTime, 20, L&quot;%Y-%m-%d_%H-%M-%S&quot;, tmLocalTime);
    swprintf(filename, MAX_FILE_NAME, L&quot;file_list_%s_ed2k-%c_%s.htm&quot;,
    L&quot;$Dir&quot;, ed2k, cLocalTime);
    wcsftime(cLocalTime, 20, L&quot;%Y-%m-%d %H:%M:%S&quot;, tmLocalTime);

    if (f = _wfopen(filename, L&quot;wb&quot;) == NULL)
    {
        fwprintf(stdout, L&quot;Could not open file: %s\n&quot;, filename);
        free(directory);
        return 1;
    }

    writeHeaderW(directory, cLocalTime);

    if (parse_directory(directory) != 0)
    {
        fwprintf(stdout, L&quot;Could not parse directory: %s\n&quot;, directory);
    }

    free(directory);

    writeFooterW();

    fclose(f);

    return 0;
}
</code></pre>
<p>Ich hoffe ich hab da jetzt nicht zu viel Sourcecode gepostet, aber das Problem liegt irgendwie immer an einer anderen Stelle.</p>
<p>MFG<br />
Long Long Double</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/192475/dateien-verzeichnisse-auflisten-unicode</link><generator>RSS for Node</generator><lastBuildDate>Wed, 01 Jul 2026 01:45:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/192475.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 14 Sep 2007 20:12:44 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Dateien&#x2F;Verzeichnisse auflisten (Unicode) on Fri, 14 Sep 2007 20:14:38 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich versuche gerade ein Programm zu schreiben, das alle Dateien und Unterordner in einem Ordner in eine html datei schreibt und auch mit Unicode zurecht kommt.</p>
<p>Allerdings hab ich so ein paar Probleme:</p>
<p>Ich benutze WinXP 32bit und GCC (MinGW) 3.4.2 und NTFS, daher die #defines</p>
<p>1. Die Funktion &quot;swprintf&quot; wird in meiner Headerdatei so &quot;int swprintf (wchar_t *s, const wchar_t *format, ...);&quot;<br />
und auf vielen Internetseiten so &quot;int swprintf(wchar_t *s, size_t n, const wchar_t *format, ...);&quot; deklariert.<br />
2. Bekomme ich beim 2. swprintf Aufruf (in main) einen Bufferoverflow bzw. einen SIGSEGV<br />
3. Wenn ich das size_t weglasse, bekomme ich dafür beim fputws() in writeHeaderW() einen bufferoverflow<br />
3. Wenn man das ganze mit Visual Studio kompiliert, läuft das ganze zwar, aber nach ein paar dateien bekommt man einen &quot;Stackoverflow&quot;<br />
EDIT: Das Problem wurde jetzt (glaub ich) gelöst, indem ich den Speicher &quot;mallociere&quot;</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;
#include &lt;time.h&gt;

#define MAX_PATH_SIZE 32767 + 1
#define MAX_FILE_NAME 255 + 1

wchar_t ed2k;
FILE *f;

int parse_directory(wchar_t *directory)
{
    wchar_t *currentDirectory = (wchar_t *)malloc(MAX_PATH_SIZE * sizeof(wchar_t));
    wchar_t *nextDirectory = (wchar_t *)malloc(MAX_PATH_SIZE * sizeof(wchar_t));
    HANDLE hFind = INVALID_HANDLE_VALUE;
    WIN32_FIND_DATAW lpFindFileData;

    fwprintf(f, L&quot;Current directory: %s\n&quot;, directory);

    swprintf(currentDirectory, MAX_PATH_SIZE, L&quot;%s\\*&quot;, directory);

    hFind = FindFirstFileW(currentDirectory, &amp;lpFindFileData);

    if (hFind == INVALID_HANDLE_VALUE)
    {
        fwprintf(f, L&quot;Invalid file handle. Error#: %lu.\n&quot;, GetLastError());
        free(currentDirectory);
        free(nextDirectory);
        return 1;
    }

    do
    {
        if (!(lpFindFileData.cFileName[0] == L'.'
            &amp;&amp; ((lpFindFileData.cFileName[1] == L'.'
            &amp;&amp; lpFindFileData.cFileName[2] == L'\0')
            || lpFindFileData.cFileName[1] == L'\0')))
        {
            if (lpFindFileData.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY)
            {
                fwprintf(f, L&quot;Dir: %s\n&quot;, lpFindFileData.cFileName);
                swprintf(nextDirectory, MAX_PATH_SIZE, L&quot;%s\\%s&quot;,
                directory, lpFindFileData.cFileName);
                parse_directory(nextDirectory);
            }
            else
            {
                fwprintf(f, L&quot;File: %s\n&quot;, lpFindFileData.cFileName);
            }
        }
    }
    while (FindNextFileW(hFind, &amp;lpFindFileData) != 0);

    free(currentDirectory);
    free(nextDirectory);

    FindClose(hFind);

    if (GetLastError() != ERROR_NO_MORE_FILES)
    {
        fwprintf(f, L&quot;FindNextFile error. Error is %lu.\n&quot;, GetLastError());
        return 1;
    }

    return 0;
}

void writeHeaderW(wchar_t *directory, wchar_t *cLocalTime)
{
    fputws(L&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;utf-8\&quot;?&gt;\n&quot;
           L&quot;&lt;!DOCTYPE html PUBLIC \&quot;-//W3C//DTD XHTML 1.1//EN\&quot; &quot;
           L&quot;\&quot;http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\&quot;&gt;\n&quot;
           L&quot;&lt;html&gt;\n&quot;
           L&quot;&lt;head&gt;\n&quot;, f);

    fwprintf(f, L&quot;&lt;title&gt;Contents of: \&quot;%s\&quot; (\&quot;%s\&quot;)&lt;/title&gt;\n&quot;
                L&quot;&lt;link rel=\&quot;stylesheet\&quot; href=\&quot;style.css\&quot; type=\&quot;text/css\&quot;&quot;
                L&quot; title=\&quot;Default-Style\&quot; media=\&quot;screen\&quot; /&gt;\n&quot;
                L&quot;&lt;/head&gt;\n&quot;
                L&quot;&lt;body&gt;\n&quot;, directory, cLocalTime);

    fwprintf(f, L&quot;&lt;h1&gt;Contents of: \&quot;%s\&quot; (\&quot;%s\&quot;)&lt;/h1&gt;\n&quot;
                L&quot;&lt;table&gt;\n&quot;
                L&quot;  &lt;tr&gt;\n&quot;
                L&quot;    &lt;th&gt;Name&lt;/th&gt;\n&quot;
                L&quot;    &lt;th&gt;Filesize&lt;/th&gt;\n&quot;
                L&quot;    &lt;th&gt;InodeChangeTime&lt;/th&gt;\n&quot;
                L&quot;    &lt;th&gt;ModificationTime&lt;/th&gt;\n&quot;
                L&quot;    &lt;th&gt;AccessTime&lt;/th&gt;\n&quot;, directory, cLocalTime);

    if (ed2k == L'1')
    {
        fputws(L&quot;    &lt;th&gt;ed2k-Hash&lt;/th&gt;\n&quot;, f);
    }
    fputws(L&quot;  &lt;/tr&gt;\n&quot;, f);
}

void writeFooterW()
{
    fputws(L&quot;&lt;/table&gt;\n&quot;
           L&quot;&lt;/body&gt;\n&quot;
           L&quot;&lt;/html&gt;&quot;, f);
}

int main(int argc, char **argv)
{
    if (argc != 3)
    {
        fprintf(stdout, &quot;usage: %s [directory] [ed2k 0/1]\n&quot;, argv[0]);
        return 1;
    }

    wchar_t *directory = (wchar_t *) malloc(MAX_PATH_SIZE * sizeof(wchar_t));
    wchar_t filename[MAX_FILE_NAME], cLocalTime[20];
    swprintf(directory, MAX_PATH_SIZE, L&quot;\\\\?\\%s&quot;, L&quot;C:&quot;); // argv[1]
    ed2k = L'0'; // argv[2]
    time_t tTime;
    time(&amp;tTime);
    struct tm *tmLocalTime = localtime(&amp;tTime);
    wcsftime(cLocalTime, 20, L&quot;%Y-%m-%d_%H-%M-%S&quot;, tmLocalTime);
    swprintf(filename, MAX_FILE_NAME, L&quot;file_list_%s_ed2k-%c_%s.htm&quot;,
    L&quot;$Dir&quot;, ed2k, cLocalTime);
    wcsftime(cLocalTime, 20, L&quot;%Y-%m-%d %H:%M:%S&quot;, tmLocalTime);

    if (f = _wfopen(filename, L&quot;wb&quot;) == NULL)
    {
        fwprintf(stdout, L&quot;Could not open file: %s\n&quot;, filename);
        free(directory);
        return 1;
    }

    writeHeaderW(directory, cLocalTime);

    if (parse_directory(directory) != 0)
    {
        fwprintf(stdout, L&quot;Could not parse directory: %s\n&quot;, directory);
    }

    free(directory);

    writeFooterW();

    fclose(f);

    return 0;
}
</code></pre>
<p>Ich hoffe ich hab da jetzt nicht zu viel Sourcecode gepostet, aber das Problem liegt irgendwie immer an einer anderen Stelle.</p>
<p>MFG<br />
Long Long Double</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1365556</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1365556</guid><dc:creator><![CDATA[long long double]]></dc:creator><pubDate>Fri, 14 Sep 2007 20:14:38 GMT</pubDate></item><item><title><![CDATA[Reply to Dateien&#x2F;Verzeichnisse auflisten (Unicode) on Fri, 14 Sep 2007 20:42:23 GMT]]></title><description><![CDATA[<p>Vielleicht hast Du einen Reparse-Point auf Deiner Festplatte... sowas kann schnell zu einer Endlosschleife führen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1365568</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1365568</guid><dc:creator><![CDATA[Jochen Kalmbach]]></dc:creator><pubDate>Fri, 14 Sep 2007 20:42:23 GMT</pubDate></item><item><title><![CDATA[Reply to Dateien&#x2F;Verzeichnisse auflisten (Unicode) on Fri, 14 Sep 2007 20:51:23 GMT]]></title><description><![CDATA[<p>Bei GCC:<br />
Wenn das Programm überhaupt so weit laufen würde ... der erste Bufferflow kommt schon bevor ich die &quot;parse_directory()&quot; funktion aufrufe, wie ich oben geschrieben habe.</p>
<p>Bei Visual Studio:<br />
Mit VS geht seltsamerweise jetzt alles, also muss das ganze irgendwie am Compiler liegen, und nicht am Sourcecode.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1365574</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1365574</guid><dc:creator><![CDATA[long long double]]></dc:creator><pubDate>Fri, 14 Sep 2007 20:51:23 GMT</pubDate></item></channel></rss>