Загрузите файлы XLS в цикле Cpp - PullRequest
0 голосов
/ 13 июля 2020

Разработка на основе моего предыдущего вопроса , хотела обобщить поведение, чтобы загружать файлы в пределах определенного c диапазона данных.

Итак, изменил код, чтобы он выглядел так

#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <ctime>
#include <iostream>
#include <iostream>
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <windows.h>

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

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);
    }
}

struct mytm : std::tm {        // inherit "tm" to extend it a little
    mytm(int year, int month, int day) : tm{}, current{} {
        tm_year = year - 1900;
        tm_mon = month - 1;
        tm_mday = day;
        tm_hour = 12;
        current = mktime(this);
    }

    void add_days(int days) {
        current += days * 60 * 60 * 24;
        std::memcpy(this, std::localtime(&current), sizeof(std::tm));
    }

    std::string strftime(const char* format) {
        std::string retval(128, ' '); // a suitable sized buffer
        std::size_t len = std::strftime(&retval[0], retval.size(), format, this);

        retval.resize(len);
        return retval;
    }

    std::time_t current;
};

int main(void) {

    mytm date(2007, 1, 1);
    
    for (int i = 0; i < 10; ++i) {
        Sleep(1000);
        std::string url =
            date.strftime("http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T=CRG&P=%d-%m-%Y&variation=PT");

        std::string file_location =
            date.strftime("C:/Users/molecoder/Desktop/file_%d_%m_%Y.xls");

        // Pass std::string to a function that needs const char* - https://stackoverflow.com/a/347959/5675325
        const char * url_final = url.c_str();
        const char * file_location_final = file_location.c_str();

        downloadFile(url_final, file_location_final);
    }
}

При этом загружается файл XLS с правильным именем и данными (поэтому он правильно запрашивает URL-адрес)

XLS работает нормально

это происходит только для начальной даты - день, месяц и год не меняются.

Ответы [ 2 ]

1 голос
/ 14 июля 2020

Как он говорит , проблема в том, что дата не изменялась, поэтому мне нужно было обновить date, например,

#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <ctime>
#include <iostream>
#include <stdio.h>
#include <curl/curl.h>
#include <string>
#include <windows.h>
#include <cstdlib>

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

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);
    }
}

struct mytm : std::tm {        // inherit "tm" to extend it a little
    mytm(int year, int month, int day) : tm{}, current{} {
        tm_year = year - 1900;
        tm_mon = month - 1;
        tm_mday = day;
        tm_hour = 12;
        current = mktime(this);
    }

    void add_days(int days) {
        current += days * 60 * 60 * 24;
        std::memcpy(this, std::localtime(&current), sizeof(std::tm));
    }

    std::string strftime(const char* format) {
        std::string retval(128, ' '); // a suitable sized buffer
        std::size_t len = std::strftime(&retval[0], retval.size(), format, this);

        retval.resize(len);
        return retval;
    }

    std::time_t current;
};

int main(void) {
    
    for (int year : {2018, 2019})
        for (int month : {1, 11})
            for (int day : {1, 31}) {
                std::cout << 
                    mytm(year, month, day)
                    .strftime("http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T=CRG&P=%d-%m-%Y&variation=PT")
                    << std::endl;

                Sleep(1000);
                
                std::string url =
                    mytm(year, month, day).strftime("http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T=CRG&P=%d-%m-%Y&variation=PT");

                std::string file_location =
                    mytm(year, month, day).strftime("C:/Users/molecoder/Desktop/file_%d_%m_%Y.xls");

                // Pass std::string to a function that needs const char* - https://stackoverflow.com/a/347959/5675325
                const char * url_final = url.c_str();
                const char * file_location_final = file_location.c_str();

                downloadFile(url_final, file_location_final);
            }


}

Обратите внимание, что некоторые файлы выиграли ' Если есть данные, то будет получен следующий результат:

Исправить ошибку.REN_CRG_Total :: pr_REN_CRG_SelectTotalByDate_Excel :: Произошла ошибка.

Хм ...

1 голос
/ 13 июля 2020

Вы назначаете date только один раз:

mytm date(2007, 1, 1);

И никогда не изменяете это. Так что, конечно, это не изменится.

Обновите date перед форматированием нового имени файла.

...