0

I am trying to fetch stock quotes from yahoo finance in C. I know how to get the file i want, it's simple enough you input this URL in your browser for example : download.finance.yahoo.com/d/quotes.csv?s=YHOO+GOOG+MSFT&f=sl1d1t1c1hgvbap2 and it automatically download the file. My problem is that i wanted to fetch this file using a C program, I've found libcurl that apparently enable you to do that but my effort linked to nothing. I would like to know how to fetch such a file using libcurl or if it is not possible with libcurl how to fetch a file from a URL.

This is my libcurl code to fetch the file that didn't work (no error just an empty file at the end) :

#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <string.h>

size_t write_data(void* ptr,size_t size,size_t nmemb,FILE* stream){
    size_t written;
    written=fwrite(ptr,size,nmemb,stream);
}

int main(void){
    CURL* curl;
    FILE* fp;
    CURLcode res;
    char* url="http://download.finance.yahoo/d/quotes.csv?s=YHOO+GOOG+MSFT&f=sl1d1t1c1hgvbap2";
    //char* url="https://marketviewer.equiduct.com";
    char outfilename[FILENAME_MAX]="test.txt";
    curl=curl_easy_init();
    if(curl){
        fp=fopen(outfilename,"wb");
        curl_easy_setopt(curl,CURLOPT_URL, url);
        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data);
        curl_easy_setopt(curl,CURLOPT_WRITEDATA,fp);
        res=curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);
    }
    else {
        printf("Error !!!\n");
    }
    return 0;
}
PiggyGenius
  • 443
  • 2
  • 9
  • 25

1 Answers1

0

try using their example for getting a file:

http://curl.haxx.se/libcurl/c/getinmemory.html

description: describes how you can use the callback system to fetch documents into a ram buffer with no file writing necessary.

you should save the data to a file if needed.

antonpuz
  • 3,256
  • 4
  • 25
  • 48
  • This link shows how to store data in memory and not in a file. It seems that i use the same functions to get data, the difference is that they store the data in memory. – PiggyGenius Nov 23 '14 at 16:12
  • you can then store the buffer to file – antonpuz Nov 23 '14 at 16:13
  • Yes, what I meant is that apart from the memory storage they are using what I am using to fetch data and store it. I don't see the difference in the fetching, am I missing something ? – PiggyGenius Nov 23 '14 at 16:15
  • you may use the `CURLOPT_WRITEDATA` to pass a FILE* and modify the code so it will write directly to the file. or you may write the entire buffer to file with write or equivalent. – antonpuz Nov 23 '14 at 16:17
  • I am using that function, look at the code I posted. I am a beginner on libcurl so don't be afraid to tell me obvious things if I am missing something. – PiggyGenius Nov 23 '14 at 16:19
  • The problem is not the code, this code works to download a common html file, the problem might be that i want a csv file ? – PiggyGenius Nov 23 '14 at 16:30