<?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[cUrl und c++ download problem]]></title><description><![CDATA[<p>Hallo,<br />
Ich habe versucht eine Updatefunktion für eins meiner Programme zu schreiben , jedoch scheint curl nur einen Download pro programmausführung zuzulassen, da das Programm sobald der zweite Download startet mit einem &quot;run failed&quot; abstürtzt.</p>
<p>Der code:</p>
<pre><code class="language-cpp">#include &lt;curl/curl.h&gt;
        #include &lt;cstdio&gt;
        #include &lt;iostream&gt;
        #include &lt;stdio.h&gt;
        #include &lt;stdlib.h&gt;
        #include &lt;string&gt;
        #include &lt;string.h&gt;
        #include &lt;fstream&gt;

        using namespace std;    

        size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
         size_t written;
         written = fwrite(ptr, size, nmemb, stream);
         return written;
        }

        char* version = &quot;0.0.0&quot;;
        void downloadFile(const char* url, const char* fname) {
         CURL *curl;
         FILE *fp;
         CURLcode res;
         curl = curl_easy_init();
         if (curl){
                fp = fopen(fname, &quot;wb&quot;);
                curl_easy_setopt(curl, CURLOPT_URL, url);
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
                res = curl_easy_perform(curl);
                curl_easy_cleanup(curl);
                fclose(fp);
            }
        }

        string get_online_version()
        {
                char* appdata = getenv(&quot;APPDATA&quot;);
                char* newVersionPath = appdata;

                strcat( newVersionPath,&quot;\\test\\onlineversion.txt&quot; );

                downloadFile( &quot;bla.tdl/update/version.txt&quot;,
                newVersionPath) ;

                string buffer;

                ifstream file(newVersionPath);
                getline(file,buffer);

                file.close();
                return buffer;

        }

        bool check_version()
        {

            string online_version = get_online_version();
            cout &lt;&lt; &quot;This: &quot; &lt;&lt; version &lt;&lt; endl;
            cout &lt;&lt; &quot;New : &quot; &lt;&lt; online_version &lt;&lt; endl;

            if (version &lt; online_version){

                return true;

            }

            else {

                return false;

            }

        }

        bool update() {

                char* appdata = getenv(&quot;APPDATA&quot;);
                char* newVersionPath = appdata;

                strcat( newVersionPath,&quot;\\test\\setup.exe&quot; );            

                downloadFile( &quot;bla.tdl/update/setup.exe&quot;,
                newVersionPath);

                //system(newVersionPath);

                return true;
        }

        int main()
        {
            curl_global_init(CURL_GLOBAL_ALL);

            if(!check_version()){

                cout &lt;&lt; &quot;Everything up to date&quot; &lt;&lt; endl;

                return 0;
            }
            else{
                cout &lt;&lt; &quot;Update verfügbar&quot; &lt;&lt; endl;
                cout &lt;&lt; &quot;Do you want to update (yes)?:&quot;;
                string line;
                getline(cin, line);
                if ( line.empty() )
                {
                    update();
                    return 0;
                }

                else if (line == &quot;yes&quot;){
                    update();
                    return 0;
                }

                else if (line == &quot;no&quot;){
                    cout &lt;&lt; &quot;no&quot;;
                }
            }
        }
</code></pre>
<p>Danke für eure Hilfe<br />
--Crosant--</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/305957/curl-und-c-download-problem</link><generator>RSS for Node</generator><lastBuildDate>Sat, 08 Aug 2026 12:24:58 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/305957.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 13 Jul 2012 16:01:50 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to cUrl und c++ download problem on Fri, 13 Jul 2012 16:07:29 GMT]]></title><description><![CDATA[<p>Hallo,<br />
Ich habe versucht eine Updatefunktion für eins meiner Programme zu schreiben , jedoch scheint curl nur einen Download pro programmausführung zuzulassen, da das Programm sobald der zweite Download startet mit einem &quot;run failed&quot; abstürtzt.</p>
<p>Der code:</p>
<pre><code class="language-cpp">#include &lt;curl/curl.h&gt;
        #include &lt;cstdio&gt;
        #include &lt;iostream&gt;
        #include &lt;stdio.h&gt;
        #include &lt;stdlib.h&gt;
        #include &lt;string&gt;
        #include &lt;string.h&gt;
        #include &lt;fstream&gt;

        using namespace std;    

        size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
         size_t written;
         written = fwrite(ptr, size, nmemb, stream);
         return written;
        }

        char* version = &quot;0.0.0&quot;;
        void downloadFile(const char* url, const char* fname) {
         CURL *curl;
         FILE *fp;
         CURLcode res;
         curl = curl_easy_init();
         if (curl){
                fp = fopen(fname, &quot;wb&quot;);
                curl_easy_setopt(curl, CURLOPT_URL, url);
                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
                curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
                res = curl_easy_perform(curl);
                curl_easy_cleanup(curl);
                fclose(fp);
            }
        }

        string get_online_version()
        {
                char* appdata = getenv(&quot;APPDATA&quot;);
                char* newVersionPath = appdata;

                strcat( newVersionPath,&quot;\\test\\onlineversion.txt&quot; );

                downloadFile( &quot;bla.tdl/update/version.txt&quot;,
                newVersionPath) ;

                string buffer;

                ifstream file(newVersionPath);
                getline(file,buffer);

                file.close();
                return buffer;

        }

        bool check_version()
        {

            string online_version = get_online_version();
            cout &lt;&lt; &quot;This: &quot; &lt;&lt; version &lt;&lt; endl;
            cout &lt;&lt; &quot;New : &quot; &lt;&lt; online_version &lt;&lt; endl;

            if (version &lt; online_version){

                return true;

            }

            else {

                return false;

            }

        }

        bool update() {

                char* appdata = getenv(&quot;APPDATA&quot;);
                char* newVersionPath = appdata;

                strcat( newVersionPath,&quot;\\test\\setup.exe&quot; );            

                downloadFile( &quot;bla.tdl/update/setup.exe&quot;,
                newVersionPath);

                //system(newVersionPath);

                return true;
        }

        int main()
        {
            curl_global_init(CURL_GLOBAL_ALL);

            if(!check_version()){

                cout &lt;&lt; &quot;Everything up to date&quot; &lt;&lt; endl;

                return 0;
            }
            else{
                cout &lt;&lt; &quot;Update verfügbar&quot; &lt;&lt; endl;
                cout &lt;&lt; &quot;Do you want to update (yes)?:&quot;;
                string line;
                getline(cin, line);
                if ( line.empty() )
                {
                    update();
                    return 0;
                }

                else if (line == &quot;yes&quot;){
                    update();
                    return 0;
                }

                else if (line == &quot;no&quot;){
                    cout &lt;&lt; &quot;no&quot;;
                }
            }
        }
</code></pre>
<p>Danke für eure Hilfe<br />
--Crosant--</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2232515</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2232515</guid><dc:creator><![CDATA[Crosant]]></dc:creator><pubDate>Fri, 13 Jul 2012 16:07:29 GMT</pubDate></item></channel></rss>