cURL / Mailing Lists / curl-library / Single Mail

curl-library

CURLOPT_WRITEFUNCTION; Function not writing all data to disc file?

From: Spencer Elliott <fm_reborn_at_hotmail.com>
Date: Thu, 13 Jun 2013 16:05:47 +0100

Dear all,

 

 

I am very new to LibCurl, and am struggling with the CURLOPT_WRITEFUNCTION
function.

I have included my source code below, but essentially my issue is here:

 

/*

                                printf("\n");

                                printf(response.c_str());

                                printf("\n");

                                LogToDisk(response);

*/

 

The function that stores the data to a string seems to print the entire
string out perfectly fine on the console using “printf”. However, when it
saves to disc file, there is a large amount of text missing. Can anyone help
me understand what I am doing wrong?

 

I noticed that there is an extremely long string in the html of the page I
am going to, and this is also where is just stops working…

 

Here is an example of the last line where the file stops writing and gives
up (I put the last part in bold), it should continue waaaay past this…:

 

&gt;
(None)&nbsp;Become&nbsp;a&nbsp;member&nbsp;of&nbsp;our&nbsp;community&nbsp;t
o&nbsp;get&nbsp;inside&nbsp;information&nbsp;and&nbsp;prizes!<br>&gt;
(None)&nbsp;We&nbsp;have&nbsp;5&nbsp;other&nbsp;servers&nbsp;up&nbsp;and&nbs
p;running!<br>&gt;
(None)&nbsp;Check&nbsp;out&nbsp;the&nbsp;Doom&nbsp;24/7&nbsp;Server!<br>&gt;
(None)&nbsp;Join&nbsp;our&nbsp;Forums&nbsp;at&nbsp;RenCorner.ca!<br>&gt;
(None)&nbsp;Too&nbsp;Easy?&nbsp;Join&nbsp;the&nbsp;RenCorner&nbsp;Killing&nb
sp;Floor&nbsp;-&nbsp;Hard&nbsp;Server!<br>&gt;
(None)&nbsp;Join&nbsp;our&nbsp;Teamspeak&nbsp;3&nbsp;at&nbsp;Ts.RenCorner.ca
!<br>&gt;
(None)&nbsp;Join&nbsp;our&nbsp;IRC&nbsp;at&nbsp;irc.RenCorner.ca!&nbsp;#RC-K
F!<br>&gt; (None)&nbsp;Become&nbsp;a&nbsp;member&nbsýÁKª`ôT

 

 

 

size_t write_to_string(void *ptr, size_t size, size_t count, void *stream)

{

       ((std::string*)stream)->append((char*)ptr, 0, size*count);

       return size*count;

}

 

void LogToDisc(std::string str)

{

       std::ofstream FP;

       FP.open("temp.txt",std::ios::trunc);

       FP << StrFormat("%s\n", str.c_str()) << std::endl;

       FP.close();

 

       std::ifstream file_in("temp.txt");

       if(!file_in)

       {

              printf("Unable to open the text file \"temp.txt\".\n");

              return;

       }

}

 

 

Void myFunction()

{

       CURL *curl;

       CURLcode res;

       curl = curl_easy_init();

       if(curl)

       {

              std::string response;

              curl_easy_setopt(curl, CURLOPT_URL,
"http://208.115.205.106:8075/ServerAdmin/current_game");

              curl_easy_setopt(curl, CURLOPT_USERAGENT, "User-Agent:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0");

              curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);

              curl_easy_setopt(curl, CURLOPT_POST, 1);

              char data[128];

              sprintf(data, "%s:%s", WebAdminUserName, WebAdminPassword);

              curl_easy_setopt(curl, CURLOPT_USERPWD, data);

              sprintf(data, "UserName=%s&Password=%s", WebAdminUserName,
WebAdminPassword);

              curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);

              curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie");

              curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie");

              curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
write_to_string);

              curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);

res = curl_easy_perform(curl);

              if(CURLE_OK == res)

              {

                     char *ct;

                     res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE,
&ct);

                     if((CURLE_OK == res) && ct)

                     {

                           printf("We received Content-Type: %s\n", ct);

                     }

                     curl_easy_cleanup(curl);

              }

              printf("\n");

              printf(response.c_str());

              printf("\n");

              LogToDisk(response); //This is the bit that goes wrong…

       }

}

 

 

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-06-13