Anyway to feed live data into c++?

I figured tha there has to be a way to feed live data into c++, but curious how to do so. I know how to feed live data to excel, now my question is, would I need to integrate excel into my c++ program to use it, if so, how?
There are many ways to do it, without having to make some kind of mutant Excel hybrid.
Moschops, thanks for the reply,

How do I feed live data then through a c++ program?
Define "feed". Normally you just use standard input.
Well let's say for example, I want to write a program that shows live scores off a basketball, football, or baseball game. How would I go about writing that?
Last edited on
Every n seconds, have it query a server storing said information
Thank you Athat and ResidentBiscuit.

Newb question, but how would the code look in order to set somethign up such as this?
What is the source of the data? Is there a website to scrape? A remote computer that responds to interrogation? A TV showing the game with a video camera pointed at it? Someone typing the scores into a keyboard?
It would be from the internet, although the second source might be interesting lol
Still too vague! For example, if you're scraping a continually updated website, you might use a library such as libCurl to fetch the page data, and then search it for
"$NAMEOFTEAMONE someNumber - someNumber $NAMEOFTEAMTWO", for example, and pull out the numerical values from that.
Last edited on
http://www.google.com/finance?q=goog&ei=vDJSUJjKOIXe0QGM0AE (I figured this would be the most pratical example). Let's say I want to display the stock (Google in this case) then have it display the stock price updating every few minutes, how would I display this?
I don't know if you are using google finance as an example, but they have an API https://developers.google.com/finance/docs/2.0/developers_guide
If it was just an example you could still look for a public API.
Here is a quick hack of some example libcurl code. It's C. If I was coding this from scratch, I'd have no problems using C (which is, after all, nearly a strict subset of C++ anyway) but I'd definitely tidy up and the parsing code where I pull out the numbers I care about is pretty ugly :p

I compiled this with the command line:

gcc 002.cpp -lcurl

where -lcurl commands the linker to link against the libcurl library.

The output is a series of numbers, ten seconds (plus fetch delay) apart, where each number is the GOOG price (currently the market is closed, so it's a constant).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#include <curl/curl.h>

#include <unistd.h> // for sleep on unix. Replace if using other OS
 
struct MemoryStruct {
  char *memory;
  size_t size;
};
 
 
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)userp;
 
  mem->memory = realloc(mem->memory, mem->size + realsize + 1);
  if (mem->memory == NULL) {
    /* out of memory! */ 
    printf("not enough memory (realloc returned NULL)\n");
    exit(EXIT_FAILURE);
  }
 
  memcpy(&(mem->memory[mem->size]), contents, realsize);
  mem->size += realsize;
  mem->memory[mem->size] = 0;
 
  return realsize;
}
 
 
int main(void)
{
  CURL *curl_handle;
 
  struct MemoryStruct chunk;
 
  chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */ 
  chunk.size = 0;    /* no data at this point */ 
 
  curl_global_init(CURL_GLOBAL_ALL);
 
  /* init the curl session */ 
  curl_handle = curl_easy_init();
 
  /* specify URL to get */ 
  curl_easy_setopt(curl_handle, CURLOPT_URL, "http://finance.google.com/finance/info?client=ig&q=NASDAQ%3aGOOG");
 
  /* send all data to this function  */ 
  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
 
  /* we pass our 'chunk' struct to the callback function */ 
  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
 
  /* some servers don't like requests that are made without a user-agent
     field, so we provide one */ 
  curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
 
  while(1)
    {
     /* get it! */ 
     curl_easy_perform(curl_handle);
    
     
    
     /*
      * Now, our chunk.memory points to a memory block that is chunk.size
      * bytes big and contains the remote file.
      *
      * Do something nice with it!
      *
      * You should be aware of the fact that at this point we might have an
      * allocated data block, and nothing has yet deallocated that data. So when
      * you're done with it, you should free() it as a nice application.
      */ 
     int i;
     for (i=0; i<chunk.size; ++i)
       {
         if (chunk.memory[i] == 'r')
    	{
             i = i+6;
    	  while (chunk.memory[i] != '\"')
    	    {
                 printf("%c", chunk.memory[i]); ++i;
    	    }
    	  printf("\n");
             break;
    	}
       }
     sleep(10);
    }
 
  /* cleanup curl stuff */ 
     curl_easy_cleanup(curl_handle);
  if(chunk.memory)
    free(chunk.memory);
 
  /* we're done with libcurl, so clean it up */ 
  curl_global_cleanup();
 
  return 0;
}

Last edited on
Topic archived. No new replies allowed.