<p>If you are intent on doing it with c++, there is a blockchain parsing library that may help you here <a href="https://code.google.com/p/blockchain/" rel="nofollow">https://code.google.com/p/blockchain/</a>. </p>

<p>Depending on your application, though, I might recommend just writing a terminal script to use the daemon. It probably won't be as fast, but I bet it would be easier to get up quickly. These commands can be called to get you the info you need:</p>

<pre>
// Gets the block at index index
./bitcoin-cli getblockhash {index}

// Gets the block using the result of the first statement
./bitcoind-cli getblock {hash} 

// The block has an array of transactions in it
for (Tx tx : block.tx) {
    ./bitcoind-cli getrawtransaction {tx} 1
}
</pre>

CC BY-SA 3.0
2014-10-29T08:34:13.223Z
Also make sure you enable `txindex=1` in your `bitcoin.conf` and run build the transaction index (`bitcoind -reindex`) before you iterate all (=also non-wallet) transactions.
behas
CC BY-SA 3.0
2014-10-29T15:26:25.810Z
Thank you very much for the answer. It is really helpful Stephen. By the way, Is there any method to retrieve the last index of the block received? If so it would be helpful for me to run a for loop to get all the blockhashes.
Rasela Don
CC BY-SA 3.0
2014-10-29T15:37:55.960Z
Glad it helped! you probably want to do something like `while $(./bitcoin-cli getblockhash(i)) != null,error`, because the best block when you start running this code won't be the best block when you finish running it. If you just want the height of your blockchain, look in `$(./bitcoin-cli getinfo)`
morsecoder
CC BY-SA 3.0
2014-10-28T17:22:23.027Z
What language is that? Or is it pseudocode?
Nick ODell
CC BY-SA 3.0
2014-10-28T17:23:32.987Z
Oh, and you have to use ./bitcoin-cli now.
Nick ODell
CC BY-SA 3.0
2014-10-28T17:25:36.940Z
Pseudo code. Yeah, you're right, I'll change.
morsecoder
CC BY-SA 3.0
2014-10-28T14:48:04.840Z
2014-10-28T17:26:02.893Z
2014-10-28T17:26:02.893Z
morsecoder
<p>In my project for one particular functionality, I want to traverse through all the <strong>Transactions</strong>. For that purpose I want to traverse through all the blocks in blockchain.</p>

<bitcoind>
C++ code for traverse though all the blocks in blockchain?
Rasela Don
# C++ code for traverse though all the blocks in blockchain?

(Asked by: username_0 on 2014-10-28)

In my project for one particular functionality, I want to traverse through all the **Transactions**. For that purpose I want to traverse through all the blocks in blockchain.

**Tags:** bitcoind


---

## 1 answers

### Answer by username_1 on 2014-10-28. Score: 3 | Accepted answer

If you are intent on doing it with c++, there is a blockchain parsing library that may help you here https://code.google.com/p/blockchain/. 
Depending on your application, though, I might recommend just writing a terminal script to use the daemon. It probably won't be as fast, but I bet it would be easier to get up quickly. These commands can be called to get you the info you need:

// Gets the block at index index
./bitcoin-cli getblockhash {index}

// Gets the block using the result of the first statement
./bitcoind-cli getblock {hash} 

// The block has an array of transactions in it
for (Tx tx : block.tx) {
    ./bitcoind-cli getrawtransaction {tx} 1
}

> **Comments:**
> **username_3**: What language is that? Or is it pseudocode?  
> **username_3**: Oh, and you have to use ./bitcoin-cli now.  
> **username_1**: Pseudo code. Yeah, you're right, I'll change.  
> **username_2**: Also make sure you enable `txindex=1` in your `bitcoin.conf` and run build the transaction index (`bitcoind -reindex`) before you iterate all (=also non-wallet) transactions.  
> **username_0**: Thank you very much for the answer. It is really helpful Stephen. By the way, Is there any method to retrieve the last index of the block received? If so it would be helpful for me to run a for loop to get all the blockhashes.  
> **username_1**: Glad it helped! you probably want to do something like `while $(./bitcoin-cli getblockhash(i)) != null,error`, because the best block when you start running this code won't be the best block when you finish running it. If you just want the height of your blockchain, look in `$(./bitcoin-cli getinfo)`  

---

