fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define BUFFER_SIZE 1024
  6.  
  7. void fetch_stock_price(const char* symbol) {
  8. char command[256];
  9. char buffer[BUFFER_SIZE];
  10.  
  11. // Replace with your own API key
  12. const char* api_key = "YOUR_API_KEY";
  13.  
  14. // Construct curl command to fetch stock price
  15. snprintf(command, sizeof(command),
  16. "curl -s \"https://w...content-available-to-author-only...e.co/query?function=GLOBAL_QUOTE&symbol=%s&apikey=%s\" > stock.json",
  17. symbol, api_key);
  18.  
  19. // Execute curl command
  20. system(command);
  21.  
  22. // Open the file for reading
  23. FILE* fp = fopen("stock.json", "r");
  24. if (!fp) {
  25. perror("Error opening file");
  26. return;
  27. }
  28.  
  29. // Read file contents into buffer
  30. while (fgets(buffer, sizeof(buffer), fp)) {
  31. // Naive parse: look for "05. price"
  32. if (strstr(buffer, "\"05. price\"")) {
  33. char* price_str = strchr(buffer, ':');
  34. if (price_str) {
  35. price_str += 2; // skip ": "
  36. price_str[strcspn(price_str, "\"")] = '\0'; // trim quotes/newlines
  37. printf("Current price of %s: %s\n", symbol, price_str);
  38. }
  39. }
  40. }
  41.  
  42. fclose(fp);
  43. }
  44.  
  45. int main() {
  46. char symbol[10];
  47.  
  48. while (1) {
  49. printf("Enter stock symbol (or 'exit' to quit): ");
  50. scanf("%s", symbol);
  51.  
  52. if (strcmp(symbol, "exit") == 0)
  53. break;
  54.  
  55. fetch_stock_price(symbol);
  56.  
  57. printf("\nWaiting 15 seconds before next input...\n");
  58. sleep(15); // Avoid hitting rate limit
  59. }
  60.  
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0.03s 25684KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 1024

void fetch_stock_price(const char* symbol) {
    char command[256];
    char buffer[BUFFER_SIZE];

    // Replace with your own API key
    const char* api_key = "YOUR_API_KEY";

    // Construct curl command to fetch stock price
    snprintf(command, sizeof(command),
        "curl -s \"https://w...content-available-to-author-only...e.co/query?function=GLOBAL_QUOTE&symbol=%s&apikey=%s\" > stock.json",
        symbol, api_key);

    // Execute curl command
    system(command);

    // Open the file for reading
    FILE* fp = fopen("stock.json", "r");
    if (!fp) {
        perror("Error opening file");
        return;
    }

    // Read file contents into buffer
    while (fgets(buffer, sizeof(buffer), fp)) {
        // Naive parse: look for "05. price"
        if (strstr(buffer, "\"05. price\"")) {
            char* price_str = strchr(buffer, ':');
            if (price_str) {
                price_str += 2;  // skip ": "
                price_str[strcspn(price_str, "\"")] = '\0';  // trim quotes/newlines
                printf("Current price of %s: %s\n", symbol, price_str);
            }
        }
    }

    fclose(fp);
}

int main() {
    char symbol[10];

    while (1) {
        printf("Enter stock symbol (or 'exit' to quit): ");
        scanf("%s", symbol);

        if (strcmp(symbol, "exit") == 0)
            break;

        fetch_stock_price(symbol);

        printf("\nWaiting 15 seconds before next input...\n");
        sleep(15);  // Avoid hitting rate limit
    }

    return 0;
}