From dac7bf496d7b093a1da6184c6427dffe497e3c0a Mon Sep 17 00:00:00 2001 From: MovsisianRG Date: Tue, 21 May 2024 22:41:31 +0300 Subject: [PATCH] code: correct exmaple work --- main.cpp | 51 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/main.cpp b/main.cpp index 948ea64..93168c0 100644 --- a/main.cpp +++ b/main.cpp @@ -21,14 +21,18 @@ Input input_data(istream &in, bool promt) size_t number_count, bin_count; if (promt) + { cerr << "Enter number count: "; + } in >> number_count; Input input; input.numbers.resize(number_count); if (promt) + { cerr << "Enter numbers: "; + } for (size_t i = 0; i < number_count; i++) { @@ -36,17 +40,31 @@ Input input_data(istream &in, bool promt) } if (promt) + { cerr << "Count of baskets: "; + } in >> input.bin_count; return input; } +size_t +write_data(void *items, size_t item_size, size_t item_count, void *ctx) +{ + size_t data_size = item_count * item_size; + + stringstream *buffer = reinterpret_cast(ctx); + char *item = reinterpret_cast(items); + //(*buffer).write(item, data_size); + buffer->write(item, data_size); + + return data_size; +} Input download(const string &address) { stringstream buffer; - //address.c_str(); + // address.c_str(); curl_global_init(CURL_GLOBAL_ALL); { @@ -55,18 +73,16 @@ Input download(const string &address) { CURLcode res; curl_easy_setopt(curl, CURLOPT_URL, address.c_str()); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer); res = curl_easy_perform(curl); - CURL *curl = curl_easy_init(); - if (curl) + + // res = curl_easy_perform(curl); + + if (res != CURLE_OK) { - CURLcode res; - /* set options */ - /* Perform the entire transfer */ - res = curl_easy_perform(curl); - /* Check for errors */ - if (res != CURLE_OK) - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); exit(1); } curl_easy_cleanup(curl); @@ -75,12 +91,17 @@ Input download(const string &address) return input_data(buffer, false); } -int -main(int argc, char* argv[]) { + + +int main(int argc, char *argv[]) +{ Input input; - if (argc > 1) { + if (argc > 1) + { input = download(argv[1]); - } else { + } + else + { input = input_data(cin, true); }