In previous post, I shared one other's blog link that fetches Sina finance data via C++ program.
http://blog.csdn.net/whunio/article/details/7784252
However, it is created on Windows and includes one Microsoft foundation class named "afxinet.h". Here, I will introduce the method fulfilled on Linux, which relies the open source library "libcurl" and this package provides professional functions of exchanging data from local and web, even for the cross-platform. About installment and employment of libcurl, there are several things worthy being noticed, (for ubuntu)
1) when compiling the cpp file, it should add the option -lcurl at the end of command, like "g++ -o stock simple3.cpp -lcurl";
2) "sudo apt-get install curl" seems only provide shell commands of curl, not included as standard library for g++ compiler, this may cause some troubles of compiling. Someone says when you installed curl rather than standard library, you should add library path when compiling, I didn't check this, but mention it here just in case you meet such a problem
3) when I failed with curl, I removed it, and then reinstall with "sudo apt-get install libcurl4-gnults-dev" instead, this works fine, just compile the cpp with -lcurl option;
The code is given as following, and the basic tasks have been achieved, 1) access url; 2) download data into local file. About the fully use of libcurl package, you may need learn more about its options if you want fulfill more features,
#include <stdio.h>
#include <curl/curl.h>
using namespace std;
int main()
{
//create pointer of curl operation
CURL *curl;
//file pointer to create file that store the data
FILE *fp;
//indiactor that can be used to judge if opening url success or not
CURLcode res;
//name of url
const char *url="http://hq.sinajs.cn/list=sh601006";
//name of file
const char outfilename[FILENAME_MAX]="sh601006.csv";
//init
curl=curl_easy_init();
if(curl)
{//open file
fp=fopen(outfilename,"wb");
//various options for curl
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_ENCODING, "");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
//clear
curl_easy_cleanup(curl);
fclose(fp);}
return (0);
}