[cUrl] Zapis do pliku
Ostatnio zmodyfikowano 2011-09-18 09:57
lukonQ Temat założony przez niniejszego użytkownika |
[cUrl] Zapis do pliku » 2011-09-17 21:57:08 Otóż mam problem ;)
Podam od razu kod :
#include <stdio.h> #include <iostream> #include <curl/curl.h> #include <string>
using namespace std;
int main() { CURL * curl; CURLcode res; curl = curl_easy_init(); if( curl ) { curl_easy_setopt( curl, CURLOPT_URL, "www.wp.pl" ); curl_easy_perform( curl ); curl_easy_cleanup( curl ); } getchar(); return 0; }
I chciałbym teraz to co pojawi się w konsoli, czyli kod źródłowy strony wp.pl zapisać do pliku. Tylko nie wiem jak się zabrać za to ;) Jest może jakaś funkcja w cUrl żeby umożliwiła mi taką operacje? Czy będę musiał zastosować jakiś inny trick? Za porady z góry dziękuje :) |
|
pekfos |
» 2011-09-18 09:50:26 huh.. dawno nie robiłem w cURLu ale mam gdzieś kod z programem który robi to czego chcesz. zaraz poszukam w archiwach :P //edit: #include <iostream> #include <conio.h> #include <fstream> #include <curl/curl.h> using namespace std;
CURL * curl; CURLcode result; fstream plik; static char errors[ CURL_ERROR_SIZE ]; static string bufor;
static int writer( char * data, size_t size, size_t nmemb, string * bufor ) { int res = 0; if( bufor != NULL ) { bufor->append( data, size * nmemb ); res = size * nmemb; } return res; }
int main() { string url = ""; string filename = ""; cout << "wprowadz URL" << endl; getline( cin, url ); cout << "nazwij plik" << endl; getline( cin, filename ); cout << "press any key to start downloading file" << endl; getch(); cout << "started.." << endl; curl = curl_easy_init(); if( curl ) { curl_easy_setopt( curl, CURLOPT_ERRORBUFFER, errors ); curl_easy_setopt( curl, CURLOPT_URL, const_cast < char *>( url.c_str() ) ); curl_easy_setopt( curl, CURLOPT_HEADER, 0 ); curl_easy_setopt( curl, CURLOPT_FOLLOWLOCATION, 1 ); curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, writer ); curl_easy_setopt( curl, CURLOPT_WRITEDATA, & bufor ); result = curl_easy_perform( curl ); curl_easy_cleanup( curl ); if( result == CURLE_OK ) { plik.open( filename.c_str(), ios::out | ios::binary ); plik.write( const_cast < char *>( bufor.c_str() ), bufor.length() ); plik.close(); cout << "downloaded!" << endl; } else cout << "error[" << result << "] -> " << errors; } else cout << "main error!"; getch(); return 0; }
dużo pisania ale jak chcesz to na podstawie tego napisałem dll z 1 funkcją pobierającą :P int download( LPCSTR url, LPCSTR file );
|
|
lukonQ Temat założony przez niniejszego użytkownika |
» 2011-09-18 09:57:26 Wielkie dzięki pekfos ;)
Temat zamykam :) |
|
« 1 » |