<?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[Datei download]]></title><description><![CDATA[<p>Ich habe mir einen Code von WinApi besorgt um eine Datei aus dem Internet auf die Festplatte zu speichern.<br />
Doch ich bekomme immer eine Fehlermeldung zu &quot; char *pFileName = PathFindFileName(szURL);&quot;. In der fehlermeldung steht, dass der Compiler nicht weiss was &quot;PathFindFileName&quot; ist. ich verwende BCB 1.<br />
Kann mir jemand weiterhelfen, was ich verändern muss, oder einen anderen Code angeben, womit eich eine Datei aus dem Internet auf die Festplatte speichern kann?</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;
#include &lt;wininet.h&gt;
#include &lt;shlwapi.h&gt;
#pragma comment(lib, &quot;wininet.lib&quot;)
#pragma comment(lib, &quot;shlwapi.lib&quot;)

// Prototyp unserer Downloadfunktion:
void GetHttpFile(const char *szURL, const char *szSavePath);

// Unsere Funktion, die eine Datei aus dem Internet runterladen kann:
void GetHttpFile(const char *szURL, const char *szSavePath)
{
    HINTERNET hInternet, hFile;
    char szBuff1[1024];
    char szBuff2[MAX_PATH];
    char szFilePath[MAX_PATH];
    bool bGO = true;
    DWORD dwReadSize, dwCurrentDown = 0;
    unsigned long iIndex = 0;
    FILE *fFile;

    if((hInternet = InternetOpen(&quot;WININET Sample Program&quot;,
                                  INTERNET_OPEN_TYPE_PRECONFIG,
                                  NULL,
                                  NULL,
                                  0)) != NULL)
    {

        if((hFile = InternetOpenUrl(hInternet,
                                    szURL,
                                    NULL,
                                    0,
                                    INTERNET_FLAG_RELOAD,
                                    0)) != NULL)
        {
            char *pFileName = PathFindFileName(szURL);
            sprintf(szFilePath, &quot;%s%s&quot;, szSavePath, pFileName);
            fFile = fopen(szFilePath, &quot;wb+&quot;);

            char szFileSize[MAX_PATH];
            DWORD dwFileSize = MAX_PATH;
            HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH, szFileSize, &amp;dwFileSize, NULL);
            long int iFileSize = atoi(szFileSize);

            while(bGO)
            {
                bGO = (bool)InternetReadFile(hFile, &amp;szBuff1, 512, &amp;dwReadSize);

                if(bGO &amp;&amp; dwReadSize == 0) break;

                szBuff1[dwReadSize] = '\0';
                fwrite(&amp;szBuff1, 1, dwReadSize, fFile);

                dwCurrentDown += dwReadSize;
                double dCurrentDownPercent = (dwCurrentDown*100.0)/iFileSize;
                int iCurrentDownPercent    = (int)dCurrentDownPercent;
                if(iCurrentDownPercent &gt; 99) iCurrentDownPercent = 100;
                sprintf(szBuff2, &quot;%i&quot;, iCurrentDownPercent);
                strcat(szBuff2, &quot;%&quot;);

                // ACHTUNG: in der Variablen szBuff2 steht hier immer die aktuelle
                // Prozent Zahl, z.B. 57%
                // Die Werte in dieser Funtkion können natürlich auch anderwertig
                // verwendet werden, z.B. kann man auch eine ProgressBar (Statusanzeige)
                // verwenden.

                MSG msg;
                while(PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
                {
                    TranslateMessage(&amp;msg);
                    DispatchMessage(&amp;msg);
                }
            }
            fclose(fFile);
            InternetCloseHandle(hFile);
            InternetCloseHandle(hInternet);
        }
        else MessageBox(0, &quot;Konnte nicht downloaden.&quot;, 0, 0);
    }
    else MessageBox(0, &quot;Konnte nicht downloaden.&quot;, 0, 0);
}

// Aufrufen können Sie die Funktion zum Runterladen einer Datei aus dem Internet
// z.B. so:
GetHttpFile(&quot;http://www.meinehomepage.de/EineDatei.zip&quot;, &quot;C:\\&quot;);

// Sollte das Programm beim Downloaden stillstehen und Sie das nicht wollen,
// so sollten Sie die Downloadfunktion als Threadfunktion verwenden.
// Die Funktion sollte dann z.B.: void __cdecl GetHttpFile(...) lauten,
// was ich aber hier nicht genauer erklären möchte. Dazu sollte man sich
// ein Tutorial oder ähnliches zu Threads ansehen.
</code></pre>
<p>edit: sfds.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/80449/datei-download</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Apr 2026 08:17:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/80449.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 21 Jul 2004 10:05:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Datei download on Wed, 21 Jul 2004 10:22:18 GMT]]></title><description><![CDATA[<p>Ich habe mir einen Code von WinApi besorgt um eine Datei aus dem Internet auf die Festplatte zu speichern.<br />
Doch ich bekomme immer eine Fehlermeldung zu &quot; char *pFileName = PathFindFileName(szURL);&quot;. In der fehlermeldung steht, dass der Compiler nicht weiss was &quot;PathFindFileName&quot; ist. ich verwende BCB 1.<br />
Kann mir jemand weiterhelfen, was ich verändern muss, oder einen anderen Code angeben, womit eich eine Datei aus dem Internet auf die Festplatte speichern kann?</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;
#include &lt;wininet.h&gt;
#include &lt;shlwapi.h&gt;
#pragma comment(lib, &quot;wininet.lib&quot;)
#pragma comment(lib, &quot;shlwapi.lib&quot;)

// Prototyp unserer Downloadfunktion:
void GetHttpFile(const char *szURL, const char *szSavePath);

// Unsere Funktion, die eine Datei aus dem Internet runterladen kann:
void GetHttpFile(const char *szURL, const char *szSavePath)
{
    HINTERNET hInternet, hFile;
    char szBuff1[1024];
    char szBuff2[MAX_PATH];
    char szFilePath[MAX_PATH];
    bool bGO = true;
    DWORD dwReadSize, dwCurrentDown = 0;
    unsigned long iIndex = 0;
    FILE *fFile;

    if((hInternet = InternetOpen(&quot;WININET Sample Program&quot;,
                                  INTERNET_OPEN_TYPE_PRECONFIG,
                                  NULL,
                                  NULL,
                                  0)) != NULL)
    {

        if((hFile = InternetOpenUrl(hInternet,
                                    szURL,
                                    NULL,
                                    0,
                                    INTERNET_FLAG_RELOAD,
                                    0)) != NULL)
        {
            char *pFileName = PathFindFileName(szURL);
            sprintf(szFilePath, &quot;%s%s&quot;, szSavePath, pFileName);
            fFile = fopen(szFilePath, &quot;wb+&quot;);

            char szFileSize[MAX_PATH];
            DWORD dwFileSize = MAX_PATH;
            HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH, szFileSize, &amp;dwFileSize, NULL);
            long int iFileSize = atoi(szFileSize);

            while(bGO)
            {
                bGO = (bool)InternetReadFile(hFile, &amp;szBuff1, 512, &amp;dwReadSize);

                if(bGO &amp;&amp; dwReadSize == 0) break;

                szBuff1[dwReadSize] = '\0';
                fwrite(&amp;szBuff1, 1, dwReadSize, fFile);

                dwCurrentDown += dwReadSize;
                double dCurrentDownPercent = (dwCurrentDown*100.0)/iFileSize;
                int iCurrentDownPercent    = (int)dCurrentDownPercent;
                if(iCurrentDownPercent &gt; 99) iCurrentDownPercent = 100;
                sprintf(szBuff2, &quot;%i&quot;, iCurrentDownPercent);
                strcat(szBuff2, &quot;%&quot;);

                // ACHTUNG: in der Variablen szBuff2 steht hier immer die aktuelle
                // Prozent Zahl, z.B. 57%
                // Die Werte in dieser Funtkion können natürlich auch anderwertig
                // verwendet werden, z.B. kann man auch eine ProgressBar (Statusanzeige)
                // verwenden.

                MSG msg;
                while(PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
                {
                    TranslateMessage(&amp;msg);
                    DispatchMessage(&amp;msg);
                }
            }
            fclose(fFile);
            InternetCloseHandle(hFile);
            InternetCloseHandle(hInternet);
        }
        else MessageBox(0, &quot;Konnte nicht downloaden.&quot;, 0, 0);
    }
    else MessageBox(0, &quot;Konnte nicht downloaden.&quot;, 0, 0);
}

// Aufrufen können Sie die Funktion zum Runterladen einer Datei aus dem Internet
// z.B. so:
GetHttpFile(&quot;http://www.meinehomepage.de/EineDatei.zip&quot;, &quot;C:\\&quot;);

// Sollte das Programm beim Downloaden stillstehen und Sie das nicht wollen,
// so sollten Sie die Downloadfunktion als Threadfunktion verwenden.
// Die Funktion sollte dann z.B.: void __cdecl GetHttpFile(...) lauten,
// was ich aber hier nicht genauer erklären möchte. Dazu sollte man sich
// ein Tutorial oder ähnliches zu Threads ansehen.
</code></pre>
<p>edit: sfds.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/565044</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/565044</guid><dc:creator><![CDATA[mailer]]></dc:creator><pubDate>Wed, 21 Jul 2004 10:22:18 GMT</pubDate></item><item><title><![CDATA[Reply to Datei download on Wed, 21 Jul 2004 10:19:10 GMT]]></title><description><![CDATA[<p>@mailer, bitte beim nächsten Mal die Code-Tags verwenden. Die benötigten Dateien hast du ja eingebunden. Wie lautet die genaue Fehlermeldung die du bekommst?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/565054</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/565054</guid><dc:creator><![CDATA[Herrmann]]></dc:creator><pubDate>Wed, 21 Jul 2004 10:19:10 GMT</pubDate></item><item><title><![CDATA[Reply to Datei download on Wed, 21 Jul 2004 22:39:23 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/6342">@Herrmann</a>. Hier sind die Fehlermeldungen.<br />
<a href="http://www.lo-net.de/fileexchange/6187_991314/fehlermeldungen.jpg" rel="nofollow">http://www.lo-net.de/fileexchange/6187_991314/fehlermeldungen.jpg</a></p>
<p>Ich kenne mich so gut wie garnicht mit C++ aus, weil ich noch Anfänger bin. Es wäre nett, wenn mir jemand ein Beispiel geben könnte (also wie der Quelltext eichtig sein müsste oder halt nen anderen der funzt) als inrgendetwas erklären mit Kopenenten hier Operatoren da, etc.</p>
<p>edit: Bitte [url]-Tags verwenden, [html] ist zum Posten von HTML-Quellcode gedacht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/565511</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/565511</guid><dc:creator><![CDATA[mailer]]></dc:creator><pubDate>Wed, 21 Jul 2004 22:39:23 GMT</pubDate></item><item><title><![CDATA[Reply to Datei download on Wed, 21 Jul 2004 23:54:40 GMT]]></title><description><![CDATA[<p>Sieht so aus als wäre deine shlwapi.h defekt oder so...<br />
Wirf da mal nen blick rein ob die funktion da irgendwo drin zu finden ist...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/565555</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/565555</guid><dc:creator><![CDATA[geeky]]></dc:creator><pubDate>Wed, 21 Jul 2004 23:54:40 GMT</pubDate></item><item><title><![CDATA[Reply to Datei download on Thu, 22 Jul 2004 03:36:28 GMT]]></title><description><![CDATA[<p>Hallo mailer,<br />
geeky hat Recht <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="🙂"
    /> Wenn du einen anderen Compiler hast z.B. VC++ kannst du deinen Code ja mal durch diesen jagen. Wenn es funktioniert liegt an den Header Dateien von deinem ja schon recht angestaubten BCB1. Schau mal ob du auf der Borland-Seite Updates für deine Entwicklungsumgebung findest.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/565565</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/565565</guid><dc:creator><![CDATA[Herrmann]]></dc:creator><pubDate>Thu, 22 Jul 2004 03:36:28 GMT</pubDate></item><item><title><![CDATA[Reply to Datei download on Thu, 22 Jul 2004 09:42:59 GMT]]></title><description><![CDATA[<p>hey!<br />
probier doch mal diesen code. sollte unter Borland, MSVC und Dev-Cpp ohne probleme kompilieren...</p>
<pre><code class="language-cpp">#include &lt;string&gt;
using namespace std;

#define DEBUG 1
#define PUFFERGROESSE 1024

class CHttpDownload {
    public:
        string host;
        int port;
        string url;
        string zieldatei;
        string antwortaufrufer;

        // ohne Proxy
        CHttpDownload(string url, string zieldatei) {
            int pos;
            if ((pos=url.find(&quot;http://&quot;))&gt;-1) {
                host=url.substr(pos+7,url.length());
                if ((pos=host.find(&quot;/&quot;))&gt;-1) {
                    host=host.substr(0,pos);
                }
            }
            else {
                if (DEBUG==1) printf(&quot;*** CHttpDownload: die URL muss mit http:// anfangen!\n&quot;);
                return;
            }
            if (DEBUG==1) printf(&quot;CHttpDownload: Der Host ist: %s\n&quot;,host.c_str());
            this-&gt;host=host;
            this-&gt;port=80;
            this-&gt;url=url;
            this-&gt;zieldatei=zieldatei;
            httpDownload();
        }

        // mit Proxy
        CHttpDownload(string url, string zieldatei, string proxy, int port) {
            this-&gt;host=proxy;
            this-&gt;port=port;
            this-&gt;url=url;
            this-&gt;zieldatei=zieldatei;
            httpDownload();
        }

        ~CHttpDownload() {
        }

        void httpDownload () {
            struct sockaddr_in server;
            struct hostent *host_info;
            unsigned long addr;
            int bzaehler;
            int sock;
            char buffer[PUFFERGROESSE];

            int pos;

            /* Initialisiere TCP für Windows (&quot;winsock&quot;) */
            short wVersionRequested;
            WSADATA wsaData;
            wVersionRequested=MAKEWORD(1,1);
            if (WSAStartup(wVersionRequested,&amp;wsaData)!=0) {
                if (DEBUG==1) printf(&quot;*** CHttpDownload: Konnte Windows-Sockets nicht initialisieren! Fehlercode: %d\n&quot;,WSAGetLastError());
                WSACleanup();
                return;
            }

            /* Erzeuge das Socket */
            sock=socket(PF_INET,SOCK_STREAM,0);
            if (sock&lt;0) {
                if (DEBUG==1) printf(&quot;*** CHttpDownload: Konnte Socket nicht erzeugen! Fehlercode: %d\n&quot;,WSAGetLastError());
                closesocket(sock);
                WSACleanup();
                return;
            }

            /* Erzeuge die Socketadresse des Servers
               Sie besteht aus Typ, IP-Adresse und Portnummer */
            memset(&amp;server,0,sizeof(server));
            if ((addr=inet_addr(host.c_str()))!=INADDR_NONE) {
                /* argv[1] ist eine numerische IP-Adresse */
                memcpy((char *)&amp;server.sin_addr,&amp;addr,sizeof(addr));
            }
            else {
                /* Wandle den Servernamen in eine IP-Adresse um */
                host_info=gethostbyname(host.c_str());
                if (NULL==host_info) {
                    if (DEBUG==1) printf(&quot;*** CHttpDownload: Host %s ist unbekannt! Fehlercode: %d\n&quot;,host.c_str(),WSAGetLastError());
                    closesocket(sock);
                    WSACleanup();
                    return;
                }
                memcpy((char *)&amp;server.sin_addr,host_info-&gt;h_addr,host_info-&gt;h_length);
            }

            server.sin_family=AF_INET;
            server.sin_port=htons(port);

            /* Baue die Verbindung zum Server auf */
            if (connect(sock,(struct sockaddr*)&amp;server,sizeof(server))&lt;0) {
                if (DEBUG==1) printf(&quot;*** CHttpDownload: Konnte Host %s nicht erreichen! Fehlercode: %d\n&quot;,host.c_str(),WSAGetLastError());
                closesocket(sock);
                WSACleanup();
                return;
            }

            HANDLE hZieldatei=CreateFile(zieldatei.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
            if (hZieldatei==NULL) {
                if (DEBUG==1) printf(&quot;*** CHttpDownload: Konnte Zieldatei %s nicht erstellen! Fehlercode: %d\n&quot;,zieldatei.c_str(),GetLastError());
                CloseHandle(hZieldatei);
                closesocket(sock);
                WSACleanup();
                return;
            }

            string getanfrage=&quot;GET &quot;+url+&quot;\r\nHost: &quot;+host+&quot;\r\nRange: bytes=0-\r\n\r\n&quot;;

            if (DEBUG==1) printf(&quot;CHttpDownload: GET-Anfrage:\n%s&quot;,getanfrage.c_str());

            send(sock,getanfrage.c_str(),strlen(getanfrage.c_str()),0);

            string antwort=&quot;&quot;;
            bool anfang=true;
            long dateilaenge=0;
            long erhalten=0;
            int headerlaenge=0;
            do {
                memset(&amp;buffer,0,sizeof(buffer));
                if ((bzaehler=recv(sock,buffer,sizeof(buffer),0))!=0) {

                    // um ein Abschneiden der Daten durch 0x00 (NULL) zu vermeiden:
                    for (int i=0; i&lt;PUFFERGROESSE; i++) antwort=antwort+buffer[i];

                    if (anfang) {
                        if ((pos=antwort.find(&quot;HTTP/1.1 200 OK&quot;))&gt;-1) {
                            if ((pos=antwort.find(&quot;Content-Length:&quot;))&gt;-1) {
                                headerlaenge=antwort.length();
                                string s=antwort.substr(pos+16,antwort.length());
                                pos=s.find(&quot;\r\n&quot;);
                                s=s.substr(0,pos);
                                dateilaenge=atoi(s.c_str());
                                pos=antwort.find(&quot;\r\n\r\n&quot;);
                                antwort=antwort.substr(pos+4,antwort.length());
                                bzaehler=bzaehler-(pos+4);
                                headerlaenge=headerlaenge-antwort.length();
                                anfang=false;
                            }
                            else {
                                if (DEBUG==1) printf(&quot;*** CHttpDownload: Konnte Dateilaenge nicht ermitteln!\n%s\n&quot;,antwort.c_str());
                                closesocket(sock);
                                CloseHandle(hZieldatei);
                                DeleteFile(zieldatei.c_str());
                                WSACleanup();
                                return;
                            }
                        }
                        else {
                            if (DEBUG==1) printf(&quot;*** CHttpDownload: GET-Anfrage schlug fehl!\n%s\n&quot;,antwort.c_str());
                            closesocket(sock);
                            CloseHandle(hZieldatei);
                            DeleteFile(zieldatei.c_str());
                            WSACleanup();
                            return;
                        }
                    }

                    if (DEBUG==1) printf(&quot;CHttpDownload: Daten:\n%s\n&quot;,antwort.c_str());
                    WriteFile(hZieldatei,antwort.c_str(),bzaehler,(DWORD *)&amp;bzaehler,NULL);
                    antwort=&quot;&quot;;
                    erhalten=erhalten+bzaehler;

                    char ptemp[10]; string s;
                    memset(&amp;ptemp,0,sizeof(ptemp));
                    ltoa(erhalten,ptemp,sizeof(ptemp)); s=ptemp;
                    antwortaufrufer=&quot;\r\n&quot;+s+&quot; of &quot;;
                    memset(&amp;ptemp,0,sizeof(ptemp));
                    ltoa(dateilaenge,ptemp,sizeof(ptemp)); s=ptemp;
                    if (DEBUG==1) printf(&quot;CHttpDownload: %d von %d Bytes uebertragen.\n&quot;,erhalten,dateilaenge);
                }
            }
            while (erhalten&lt;dateilaenge);

            closesocket(sock);
            CloseHandle(hZieldatei);
            WSACleanup();
            return;
        }

    private:
};

int main() {

    CHttpDownload httpdownload(&quot;www.google.de/index.html&quot;, &quot;C:\\test.html&quot;);
    httpdownload.~CHttpDownload();

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/565760</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/565760</guid><dc:creator><![CDATA[sn0b]]></dc:creator><pubDate>Thu, 22 Jul 2004 09:42:59 GMT</pubDate></item></channel></rss>