<?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[Probleme mit FindfirstFile]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich möchte gerne die Anzahl der Dateien in einem Ordner ermitteln.<br />
Hierzu habe ich bereits folgenden Code:</p>
<pre><code>void getAnzahHdcrlDateien()
{

 WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    int nFiles = 0;

    hFind = FindFirstFile(&quot;C:\\Windows\\*&quot;, &amp;FindFileData);

    if (hFind == INVALID_HANDLE_VALUE) {
		exit(0);							
    }

    do {
        if (FindFileData.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY) {
            // aktuelles Element ist ein Verzeichnis
            // TODO: Rekursiven Aufruf implementieren
        } else {
            // aktuelles Element ist eine Datei
            nFiles++;
        }
    } while (FindNextFile(hFind, &amp;FindFileData));

    FindClose(hFind);

    printf(&quot;%d files.&quot;, nFiles);
}
</code></pre>
<p>Als Fehlermeldung kommt &quot;Das Argument vom Typ &quot;&quot;const char *&quot;&quot; ist mit dem Parameter vom Typ &quot;&quot; LPCWSTR&quot;&quot; inkompatibel.<br />
Wenn ich den Pfad mit LPCWSTR caste findet er keine Dateien.</p>
<p>Ist bestimmt ein Anfängerfehler - SORRY!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/308028/probleme-mit-findfirstfile</link><generator>RSS for Node</generator><lastBuildDate>Thu, 06 Aug 2026 06:05:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/308028.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 12 Sep 2012 19:06:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Wed, 12 Sep 2012 19:06:07 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich möchte gerne die Anzahl der Dateien in einem Ordner ermitteln.<br />
Hierzu habe ich bereits folgenden Code:</p>
<pre><code>void getAnzahHdcrlDateien()
{

 WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    int nFiles = 0;

    hFind = FindFirstFile(&quot;C:\\Windows\\*&quot;, &amp;FindFileData);

    if (hFind == INVALID_HANDLE_VALUE) {
		exit(0);							
    }

    do {
        if (FindFileData.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY) {
            // aktuelles Element ist ein Verzeichnis
            // TODO: Rekursiven Aufruf implementieren
        } else {
            // aktuelles Element ist eine Datei
            nFiles++;
        }
    } while (FindNextFile(hFind, &amp;FindFileData));

    FindClose(hFind);

    printf(&quot;%d files.&quot;, nFiles);
}
</code></pre>
<p>Als Fehlermeldung kommt &quot;Das Argument vom Typ &quot;&quot;const char *&quot;&quot; ist mit dem Parameter vom Typ &quot;&quot; LPCWSTR&quot;&quot; inkompatibel.<br />
Wenn ich den Pfad mit LPCWSTR caste findet er keine Dateien.</p>
<p>Ist bestimmt ein Anfängerfehler - SORRY!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250747</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250747</guid><dc:creator><![CDATA[MarcusG]]></dc:creator><pubDate>Wed, 12 Sep 2012 19:06:07 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Wed, 12 Sep 2012 19:43:26 GMT]]></title><description><![CDATA[<p>FindFirstFileA</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250760</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250760</guid><dc:creator><![CDATA[Ethon]]></dc:creator><pubDate>Wed, 12 Sep 2012 19:43:26 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Wed, 12 Sep 2012 19:50:31 GMT]]></title><description><![CDATA[<p>dann schreibt er als Fehlermeldung:<br />
&quot;Das Argument vom Typ &quot;WIN32_FIND_DATA *&quot; ist mit dem Parameter vom Typ &quot;LPWIN32_FIND_DATAA&quot; inkompatibel</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250761</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250761</guid><dc:creator><![CDATA[MarcusG]]></dc:creator><pubDate>Wed, 12 Sep 2012 19:50:31 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Wed, 12 Sep 2012 21:40:23 GMT]]></title><description><![CDATA[<p>MarcusG schrieb:</p>
<blockquote>
<p>Als Fehlermeldung kommt &quot;Das Argument vom Typ &quot;&quot;const char *&quot;&quot; ist mit dem Parameter vom Typ &quot;&quot; LPCWSTR&quot;&quot; inkompatibel.</p>
</blockquote>
<p>Stell Dein Projekt von UNICODE um auf MBCS.</p>
<p>Edit:<br />
Oder aber schreib:</p>
<pre><code class="language-cpp">hFind = FindFirstFile(TEXT(&quot;C:\\Windows\\*&quot;), &amp;FindFileData);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2250787</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250787</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Wed, 12 Sep 2012 21:40:23 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Wed, 12 Sep 2012 22:00:44 GMT]]></title><description><![CDATA[<p>benötige leider Unicode</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250788</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250788</guid><dc:creator><![CDATA[MarcusG]]></dc:creator><pubDate>Wed, 12 Sep 2012 22:00:44 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Wed, 12 Sep 2012 22:17:46 GMT]]></title><description><![CDATA[<p>Sorry, habe dein Edit überlesen - funktioniert!<br />
Vielen Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250792</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250792</guid><dc:creator><![CDATA[MarcusG]]></dc:creator><pubDate>Wed, 12 Sep 2012 22:17:46 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Wed, 12 Sep 2012 22:40:49 GMT]]></title><description><![CDATA[<p>Sorry, würde nochmal Eure Hilfe benötigen: Wie gibt man nun den jeweiligen Dateinamen (am liebsten mit Pfad) aus?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250796</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250796</guid><dc:creator><![CDATA[MarcusG]]></dc:creator><pubDate>Wed, 12 Sep 2012 22:40:49 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Wed, 12 Sep 2012 22:42:53 GMT]]></title><description><![CDATA[<p>selbst gefunden: FindFileData.cFileName</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250797</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250797</guid><dc:creator><![CDATA[MarcusG]]></dc:creator><pubDate>Wed, 12 Sep 2012 22:42:53 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Wed, 12 Sep 2012 22:46:19 GMT]]></title><description><![CDATA[<p>Vielleicht etwas Erklärung hierzu:<br />
Viele WinAPI Funktionen sind tatsächlich doppelt vorhandeln. Einmal mit einem A am Ende (z.B. <code>FindFirstFileA</code> ) und einmal mit einem W am Ende (z.B. <code>FindFirstFileW</code> ). Das A ist ein Hinweis auf ASCII und somit der Zeit vor Unicode. W dagegen steht für Wide und steht für die <a href="http://de.wikipedia.org/wiki/UCS-2" rel="nofollow">UCS-2</a> Kodierung.</p>
<p>Die Funktionen mit einem W am Ende erwarten Wide-Strings ( <code>wchar_t</code> , <code>std::wstring</code> , etc.). Wenn du ein Wide-String-Literal willst, dann musst du ein grosses L davor stellen. Also z.B:</p>
<pre><code class="language-cpp">hFind = FindFirstFileW(L&quot;C:\\Windows\\*&quot;, &amp;FindFileData);
//                    -^-
</code></pre>
<p>Wenn du nun z.B. <code>FindFirstFile</code> verwendest, dann ist dies ein Makro. Wenn das Makro <code>UNICODE</code> definiert ist, dann evaluiert <code>FindFirstFile</code> zu <code>FindFirstFileW</code> und ansonsten zu <code>FindFirstFileA</code> .</p>
<pre><code class="language-cpp">#ifdef UNICODE
#define FindFirstFile FindFirstFileW
#else
#define FindFirstFile FindFirstFileA
#endif
</code></pre>
<p>Man kann aber <code>FindFirstFileW</code> oder <code>FindFirstFileA</code> auch explizit verwenden.</p>
<p>Das Makro <code>TEXT</code> macht im übrigen etwas ähnliches:</p>
<pre><code class="language-cpp">#ifdef UNICODE
#define TEXT(x) L##x
#else
#define TEXT(x) x
#endif
</code></pre>
<p>MarcusG schrieb:</p>
<blockquote>
<p>Sorry, würde nochmal Eure Hilfe benötigen: Wie gibt man nun den jeweiligen Dateinamen (am liebsten mit Pfad) aus?</p>
</blockquote>
<p>Mit C++, C oder der WinAPI? Und worauf? Der Konsole?<br />
In deinem Code verwendest du nämlich C und die WinAPI, schreibst deine Frage aber ins C++ Forum <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>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250798</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250798</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Wed, 12 Sep 2012 22:46:19 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Thu, 13 Sep 2012 00:11:52 GMT]]></title><description><![CDATA[<p>Danke für die Erklärungen!</p>
<p>Nutze FindFirstFileW(L&quot;....</p>
<p>Wie kann man denn jetzt die gefundene Datei dann öffnen?<br />
Versuche die Datei mit FindFileData.cFileName an eine File Variable zu binden - funktioniert aber leider nicht......</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250804</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250804</guid><dc:creator><![CDATA[MarcusG]]></dc:creator><pubDate>Thu, 13 Sep 2012 00:11:52 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Thu, 13 Sep 2012 06:38:09 GMT]]></title><description><![CDATA[<p>MarcusG schrieb:</p>
<blockquote>
<p>funktioniert aber leider nicht......</p>
</blockquote>
<p>Das ist aber schade......</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250820</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250820</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Thu, 13 Sep 2012 06:38:09 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Thu, 13 Sep 2012 06:43:03 GMT]]></title><description><![CDATA[<p>Swordfish schrieb:</p>
<blockquote>
<p>MarcusG schrieb:</p>
<blockquote>
<p>funktioniert aber leider nicht......</p>
</blockquote>
<p>Das ist aber schade......</p>
</blockquote>
<p>Sehr schade ... eine Idee warum nicht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250821</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250821</guid><dc:creator><![CDATA[MarcusG]]></dc:creator><pubDate>Thu, 13 Sep 2012 06:43:03 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Thu, 13 Sep 2012 06:48:56 GMT]]></title><description><![CDATA[<p>MarcusG schrieb:</p>
<blockquote>
<p>eine Idee warum nicht?</p>
</blockquote>
<p>Siehe meine Signatur.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250823</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250823</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Thu, 13 Sep 2012 06:48:56 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Thu, 13 Sep 2012 06:59:00 GMT]]></title><description><![CDATA[<p>Zudem bleibt die Frage im Raum: C oder C++? Mit der WinAPI? Laut deiner E-Mail willst du C++ lernen. Dein gezeigter Code hat aber nicht viel mit C++ zu tun. Zum Rest in der E-Mail: Nein, Danke.<br />
<a href="http://www.c-plusplus.net/forum/251551" rel="nofollow">http://www.c-plusplus.net/forum/251551</a></p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250825</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250825</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Thu, 13 Sep 2012 06:59:00 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Thu, 13 Sep 2012 10:20:12 GMT]]></title><description><![CDATA[<pre><code>hFind = FindFirstFileW(L&quot;D:\\Test\\*&quot;, &amp;FindFileData); 
if (hFind == INVALID_HANDLE_VALUE) { 
    exit(0);                            
    } 

    do { 
        if (FindFileData.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY) { 
            // aktuelles Element ist ein Verzeichnis  
        } else { 
            // aktuelles Element ist eine Datei 
            nFiles++; 
		if (nFiles=1);
		{
		US.KeyFile=FindFileData.cFileName;
		}
        } 
    } while (FindNextFile(hFind, &amp;FindFileData)); 
FindClose(hFind);
</code></pre>
<p>Hier bringt mir die Zuweisung</p>
<pre><code>US.KeyFile=FindFileData.cFileName
</code></pre>
<p>den Fehler: Ein Wert vom Typ WCHAR * kann keiner Entität vom Typ char * zugewiesen werden.</p>
<p>Ich möchte &quot;eigentlich&quot; nur den ersten Dateinamen haben und diese Datei dann zum Lesen öffnen.</p>
<p>Hoffe Ihr könnt mir bitte nochmal helfen.</p>
<p>Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250880</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250880</guid><dc:creator><![CDATA[MarcusG]]></dc:creator><pubDate>Thu, 13 Sep 2012 10:20:12 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Thu, 13 Sep 2012 10:26:40 GMT]]></title><description><![CDATA[<p>MarcusG schrieb:</p>
<blockquote>
<p>Ein Wert vom Typ WCHAR * kann keiner Entität vom Typ char * zugewiesen werden.</p>
</blockquote>
<p><a href="http://msdn.microsoft.com/en-us/library/dd319072%28v=vs.85%29.aspx" rel="nofollow">MultiByteToWideChar()</a></p>
<p>Sonst:</p>
<pre><code class="language-cpp">#include &lt;iterator&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;ctime&gt;

#include &lt;windows.h&gt;

typedef std::basic_string&lt; TCHAR &gt; tstring;

#ifdef UNICODE
std::wostream &amp;tcout = std::wcout;  
#else /* UNICODE */
std::ostream &amp;tcout = std::cout;
#endif /* UNICODE */

int main()
{
	std::srand( static_cast&lt; unsigned &gt;( std::time( 0 ) ) );

	WIN32_FIND_DATA find_data = {};
	HANDLE find_handle = FindFirstFile( TEXT( &quot;*&quot; ), &amp;find_data );

	if( find_handle != INVALID_HANDLE_VALUE ) {

		std::vector&lt; tstring &gt; files;

		do {

			tcout &lt;&lt; find_data.cFileName &lt;&lt; '\n';
			files.push_back( find_data.cFileName );

		} while( FindNextFile( find_handle, &amp;find_data ) );

		tstring random_file_name = files[ std::rand() % ( files.size() - 1 ) ];
		tcout &lt;&lt; TEXT( &quot;\nContent of \&quot;&quot; ) &lt;&lt; random_file_name &lt;&lt; TEXT( &quot;\&quot;:\n&quot; );

		std::ifstream file( random_file_name );
		std::cout &lt;&lt; file.rdbuf() &lt;&lt; '\n';
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2250881</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250881</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Thu, 13 Sep 2012 10:26:40 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Thu, 13 Sep 2012 10:48:56 GMT]]></title><description><![CDATA[<p>MarcusG schrieb:</p>
<blockquote>
<p>Hier bringt mir die Zuweisung</p>
<pre><code>US.KeyFile=FindFileData.cFileName
</code></pre>
<p>den Fehler: Ein Wert vom Typ WCHAR * kann keiner Entität vom Typ char * zugewiesen werden.</p>
</blockquote>
<p>Das Gleiche wie vorher:<br />
US.KeyFile ist vom Typ char*, sollte aber vom Type TCHAR* sein ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250889</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250889</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Thu, 13 Sep 2012 10:48:56 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Thu, 13 Sep 2012 13:01:15 GMT]]></title><description><![CDATA[<p>Kompiliert ihr eigentlich wirklich noch Software ohne Unicode? Oder wieso empfiehlt ihr alles Code, welcher auch zu ISO 8859 kompatibel ist? Wieso nicht gleich ausschliesslich die Unicode Varianten verwenden?</p>
<p>Erst recht wenn MarcusG sagt, dass er Unicode verwenden muss?</p>
<p>Also nein, nicht <code>TCHAR*</code> verwenden, sondern gleich <code>wchar_t*</code> . Auch die Empfehlung zu <code>MultiByteToWideChar</code> ist seltsam. Es ist nicht nur die falsche Funktion, wenn man nach <code>char*</code> konvertieren möchte (richtig wäre <code>WideCharToMultiByte</code> ), sondern auch sehr fragewürdig sowas überhaupt zu empfehlen.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2250954</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2250954</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Thu, 13 Sep 2012 13:01:15 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit FindfirstFile on Thu, 13 Sep 2012 17:18:20 GMT]]></title><description><![CDATA[<p>Dravere schrieb:</p>
<blockquote>
<p>Erst recht wenn MarcusG sagt, dass er Unicode verwenden muss?</p>
</blockquote>
<p>Dass er Unicode verwenden muss, ihm aber niemand sagt, wie das vernünftig gemacht wird, ist doch mindestens merkwürdig. Könnte es nicht sein, dass bei denen, die ihm sagen, dass er es so machen muss, entsprechende Kenntnisse auch nur rudimentär vorhanden sind?<br />
Könnte es dann weiter nicht sein, dass man über kurz oder lang beschließt, okay, wir kriegen das nicht hin, machen wir erst mal wieder MBCS.<br />
Und dann ist er froh, wenn er TCHAR* statt wchar_t* genommen hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2251062</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2251062</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Thu, 13 Sep 2012 17:18:20 GMT</pubDate></item></channel></rss>