retrieve stock quotes via C++ program on Linux

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);
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值