c++ - Why does my function gives me "Stack around the variable 'url' was corrupted." error? -


i have function should download me website string given in argument (exacly string end of url). it's throwing me failure

stack around variable 'url' corrupted.

my code:

void download_wordnik(string word) {         string s1 = word;         std::wstring w_word_tmp1(s1.begin(), s1.end());         wstring w_word1 = w_word_tmp1;               std::wstring stemp1 = std::wstring(s1.begin(), s1.end());         lpcwstr sw1 = stemp1.c_str();          tchar url[] = text("https://www.wordnik.com/words");         wsprintf(url, text("%s\/%s\/"), url, sw1);          lpcwstr sw2 = stemp1.c_str();         tchar path[max_path];         getcurrentdirectory(max_path, path);         wsprintf(path, text("%s\\wordnik\\%s\.txt"), path, sw2);         hresult res = urldownloadtofile(null, url, path, 0, null);           // checking download         if(res == s_ok) {             printf("ok\n");         } else if(res == e_outofmemory) {             printf("buffer length invalid, or insufficient memory\n");         } else if(res == inet_e_download_failure) {             printf("url invalid\n");         } else {             printf("other error: %d\n", res);         }  } 

i'm using includes

#include <iostream> #include <fstream> #include <string> #include <vector> #include <urlmon.h> #include <regex>  #pragma comment(lib, "urlmon.lib") using namespace std; 

you're writing outside bounds of url when wsprintf it.

instead (for general formatting)

std::wostringstream urlstream; urlstream << text("https://www.wordnik.com/words/") << sw1 << text("/");  std::wstring url = urlstream.str(); 

or (simpler)

std::wstring url = std::wstring(text("https://www.wordnik.com/words/")) + sw1 + text("/"); 

you're copying variables around quite lot - far can tell, can cut code down this:

    std::wstring w_word(word.begin(), word.end());     std::wstring url = std::wstring(text("https://www.wordnik.com/words/")) + w_word + text("/");     tchar currentpath[max_path];     getcurrentdirectory(max_path, currentpath);     std::wstring path = std::wstring(currentpath) + text("\\wordnik\\") + w_word + text(".txt");     hresult res = urldownloadtofile(null, url.c_str(), path.c_str(), 0, null); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -