<p>You could simply record the value of <code>bitcoind getblockcount</code> somewhere and see when that changes by calling <code>getblockcount</code> repeatedly.</p>

CC BY-SA 3.0
2014-04-23T22:22:23.337Z
If I was running bitcoind, personally I would enable the blocknotify config setting and do away with the repetitive polling.
ChrisW
CC BY-SA 3.0
2014-03-24T20:50:28.547Z
2014-03-24T20:50:28.547Z
Luca Matteis
<pre><code>#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;stdio.h&gt;

std::string exec(char* cmd) {
    FILE* pipe = popen(cmd, "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    std::string result = "";
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            result += buffer;
    }
    pclose(pipe);
    return result;
}

int main(){
    int count;
    count = stoi(exec("bitcoind getblockcount"));
    for(;;){
        int new_count = stoi(exec("bitcoind getblockcount"));
        while(count &lt; new_count){
            std::cout &lt;&lt; "Block found." &lt;&lt; std::endl;
            count++;
        }
        system("sleep 1"); // There are other ways of sleeping. This is the quick and dirty way.
    }
    return 0;
}
</code></pre>

<p>To be fair, this is a rip off of Luca's idea and <a href="https://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c">https://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c</a>.</p>

CC BY-SA 3.0
2014-04-23T21:31:26.797Z
2014-04-23T21:31:26.797Z
2017-05-23T12:34:59.703Z
Tyler
<p>I am relatively new to C++ and I was wondering if anybody could help me out. I am looking piece of code that will loop until a block is found by a node and then when one is found print a simple output like "Block found". </p>

<p>Thanks!</p>

CC BY-SA 3.0
2014-04-23T21:19:41.020Z
Is C++ really a requirement for this? I'm an avid C++ programmer myself and I would *not* want to do this in C++. And alternative would be python or even bash. Also, what OS are you targeting?
Tyler
<api><block><nodes>
What piece of code would allow me to check if a block has been found then run an operation?
Cheese
# What piece of code would allow me to check if a block has been found then run an operation?

(Asked by: username_0 on 2014-03-24)

I am relatively new to C++ and I was wondering if anybody could help me out. I am looking piece of code that will loop until a block is found by a node and then when one is found print a simple output like "Block found".

Thanks!

**Tags:** api, block, nodes

> **Comments:**
> **username_1**: Is C++ really a requirement for this? I'm an avid C++ programmer myself and I would *not* want to do this in C++. And alternative would be python or even bash. Also, what OS are you targeting?  

---

## 2 answers

### Answer by username_2 on 2014-03-24. Score: 1

You could simply record the value of `bitcoind getblockcount` somewhere and see when that changes by calling `getblockcount` repeatedly.

> **Comments:**
> **username_3**: If I was running bitcoind, personally I would enable the blocknotify config setting and do away with the repetitive polling.  

---

### Answer by username_1 on 2014-04-23. Score: -1

#include 
#include 
#include 

std::string exec(char* cmd) {
    FILE* pipe = popen(cmd, "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    std::string result = "";
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            result += buffer;
    }
    pclose(pipe);
    return result;
}

int main(){
    int count;
    count = stoi(exec("bitcoind getblockcount"));
    for(;;){
        int new_count = stoi(exec("bitcoind getblockcount"));
        while(count < new_count){
            std::cout << "Block found." << std::endl;
            count++;
        }
        system("sleep 1"); // There are other ways of sleeping. This is the quick and dirty way.
    }
    return 0;
}

To be fair, this is a rip off of Luca's idea and https://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c.


---

