cUrl und c++ download problem



  • Hallo,
    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 "run failed" abstürtzt.

    Der code:

    #include <curl/curl.h>
            #include <cstdio>
            #include <iostream>
            #include <stdio.h>
            #include <stdlib.h>
            #include <string>
            #include <string.h>
            #include <fstream>
    
            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 = "0.0.0";
            void downloadFile(const char* url, const char* fname) {
             CURL *curl;
             FILE *fp;
             CURLcode res;
             curl = curl_easy_init();
             if (curl){
                    fp = fopen(fname, "wb");
                    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("APPDATA");
                    char* newVersionPath = appdata;
    
                    strcat( newVersionPath,"\\test\\onlineversion.txt" );
    
                    downloadFile( "bla.tdl/update/version.txt",
                    newVersionPath) ;
    
                    string buffer;
    
                    ifstream file(newVersionPath);
                    getline(file,buffer);
    
                    file.close();
                    return buffer;
    
            }
    
            bool check_version()
            {
    
                string online_version = get_online_version();
                cout << "This: " << version << endl;
                cout << "New : " << online_version << endl;
    
                if (version < online_version){
    
                    return true;
    
                }
    
                else {
    
                    return false;
    
                }
    
            }
    
            bool update() {
    
                    char* appdata = getenv("APPDATA");
                    char* newVersionPath = appdata;
    
                    strcat( newVersionPath,"\\test\\setup.exe" );            
    
                    downloadFile( "bla.tdl/update/setup.exe",
                    newVersionPath);
    
                    //system(newVersionPath);
    
                    return true;
            }
    
            int main()
            {
                curl_global_init(CURL_GLOBAL_ALL);
    
                if(!check_version()){
    
                    cout << "Everything up to date" << endl;
    
                    return 0;
                }
                else{
                    cout << "Update verfügbar" << endl;
                    cout << "Do you want to update (yes)?:";
                    string line;
                    getline(cin, line);
                    if ( line.empty() )
                    {
                        update();
                        return 0;
                    }
    
                    else if (line == "yes"){
                        update();
                        return 0;
                    }
    
                    else if (line == "no"){
                        cout << "no";
                    }
                }
            }
    

    Danke für eure Hilfe
    --Crosant--


Anmelden zum Antworten