fork download
  1. #include <iostream>
  2. #include <curl/curl.h>
  3. #include <string>
  4. #include <xlsxwriter.h>
  5. // Callback function to write fetched data into a string
  6. size_t write_callback(void *contents, size_t size, size_t nmemb, std::string *data) {
  7. size_t total_size = size * nmemb;
  8. data->append((char*)contents, total_size);
  9. return total_size;
  10. }
  11. int main() {
  12. // Initialize libcurl
  13. CURL *curl;
  14. CURLcode res;
  15. curl_global_init(CURL_GLOBAL_DEFAULT);
  16. curl = curl_easy_init();
  17. if (curl) {
  18. std::string data; // String to store fetched data
  19. // Set URL to fetch data from
  20. curl_easy_setopt(curl, CURLOPT_URL, "http://e...content-available-to-author-only...e.com/data.txt");
  21. // Set callback function to write fetched data into 'data' string
  22. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
  23. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
  24. // Perform the request
  25. res = curl_easy_perform(curl);
  26. // Check for errors
  27. if (res != CURLE_OK) {
  28. std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
  29. }
  30. // Clean up
  31. curl_easy_cleanup(curl);
  32. curl_global_cleanup();
  33. // Write fetched data into an Excel file using Libxlsxwriter
  34. lxw_workbook *workbook = workbook_new("output.xlsx");
  35. lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
  36. // Split the fetched data into rows and columns and write it to the Excel file
  37. size_t row = 0;
  38. size_t col = 0;
  39. for (char c : data) {
  40. if (c == '\n') {
  41. row++;
  42. col = 0;
  43. } else if (c == ',') {
  44. col++;
  45. } else {
  46. worksheet_write_string(worksheet, row, col, &c, NULL);
  47. }
  48. }
  49. // Close the workbook
  50. workbook_close(workbook);
  51. } else {
  52. std::cerr << "Failed to initialize libcurl!" << std::endl;
  53. }
  54. return 0;
  55. }
Success #stdin #stdout 0.02s 26156KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <curl/curl.h>
#include <string>
#include <xlsxwriter.h>
// Callback function to write fetched data into a string
size_t write_callback(void *contents, size_t size, size_t nmemb, std::string *data) {
    size_t total_size = size * nmemb;
    data->append((char*)contents, total_size);
    return total_size;
}
int main() {
    // Initialize libcurl
    CURL *curl;
    CURLcode res;
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if (curl) {
        std::string data; // String to store fetched data
        // Set URL to fetch data from
        curl_easy_setopt(curl, CURLOPT_URL, "http://e...content-available-to-author-only...e.com/data.txt");
        // Set callback function to write fetched data into 'data' string
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
        // Perform the request
        res = curl_easy_perform(curl);
        // Check for errors
        if (res != CURLE_OK) {
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
        }
        // Clean up
        curl_easy_cleanup(curl);
        curl_global_cleanup();
        // Write fetched data into an Excel file using Libxlsxwriter
        lxw_workbook  *workbook  = workbook_new("output.xlsx");
        lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
        // Split the fetched data into rows and columns and write it to the Excel file
        size_t row = 0;
        size_t col = 0;
        for (char c : data) {
            if (c == '\n') {
                row++;
                col = 0;
            } else if (c == ',') {
                col++;
            } else {
                worksheet_write_string(worksheet, row, col, &c, NULL);
            }
        }
        // Close the workbook
        workbook_close(workbook);
    } else {
        std::cerr << "Failed to initialize libcurl!" << std::endl;
    }
    return 0;
}