在 Cpp 循环中下载 XLS 文件

Download XLS files in a Cpp loop

开发,想要概括行为以便下载特定数据范围内的文件。

因此,将代码修改为如下所示

#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* - 
        const char * url_final = url.c_str();
        const char * file_location_final = file_location.c_str();

        downloadFile(url_final, file_location_final);
    }
}

虽然这会下载具有正确名称和数据的 XLS 文件(因此它正确请求 URL)

它只对初始日期如此 - 日、月和年没有变化。

您只分配给 date 一次:

mytm date(2007, 1, 1);

永远不要修改它。所以当然不会变。

在格式化新文件名之前更新 date

作为,问题是日期没有被修改,所以我需要更新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* - 
                const char * url_final = url.c_str();
                const char * file_location_final = file_location.c_str();

                downloadFile(url_final, file_location_final);
            }


}

请注意,某些文件将没有数据并会得到以下结果

Ocorreu um erro.REN_CRG_Total::pr_REN_CRG_SelectTotalByDate_Excel::Error occured.