<pre><code>#include &lt;QTimer&gt;
#include &lt;QFile&gt;

#include "BlockChain.h"

BlockChain::BlockChain ( const int start, QObject* parent ) : QFile ( parent ), blkFile ( start + 1 )
{
  QTimer::singleShot ( 0, this, SLOT ( start ( ) ) );
}
//--------------------------------------------------------------
void BlockChain::start ( )
{
  setFileName ( blkFileName ( blkFile++ ) );
  if ( !open ( QIODevice::ReadOnly ) )
  {
    _trace ( QString ( "cant open [%1]" ).arg ( fileName ( ) ) );
    getParent ( ).block ( QByteArray ( ) ); // finish signal
    deleteLater ( );
  }
  else
  {
    _trace ( QString ( "processing [%1]" ).arg ( fileName ( ) ) );
    QTimer::singleShot ( 0, this, SLOT ( next ( ) ) );
  }
}
//--------------------------------------------------------------
void BlockChain::next ( )
{
  bool lock ( true );
  if ( pos ( ) &lt; size ( ) )
  {
    quint32 magic;
    quint32 size ( read ( (char*)&amp;magic, 4 ) );
    xassert ( ( ( magic == MAGIC_ID ) || !magic ) &amp;&amp; ( size == 4 ) );
    if ( magic )
    {
      read ( (char*)&amp;size, 4 );
      xassert ( size &gt; HEADER_SIZE &amp;&amp; size &lt;= MAX_BLOCK_SIZE );
      getParent ( ).block ( read ( size ) ); // process data
      QTimer::singleShot ( 0, this, SLOT ( next ( ) ) );
      return;
    }
    else
      lock = false;
  }
  close ( );
  getParent ( ).doneFile ( lock, blkFile - 1 );
  QTimer::singleShot ( 0, this, SLOT ( start ( ) ) );
}
//--------------------------------------------------------------
const QString BlockChain::blkFileName ( const int i )
{
  return
    ( i &lt; 10 ) ? QString ( DATA_ROOT "\\blk0000%1.dat" ).arg ( i ) :
    ( i &lt; 100 ) ? QString ( DATA_ROOT "\\blk000%1.dat" ).arg ( i ) :
    QString ( DATA_ROOT "\\blk00%1.dat" ).arg ( i );
}
</code></pre>

CC BY-SA 3.0
2014-10-27T08:01:07.417Z
Thank you very much for take your time to reply me. Actually I am trying to find a way to do the enhancement in C++ core, not the qt-client. Because we dont use qt environment for development.
Rasela Don
CC BY-SA 3.0
2014-10-27T08:50:27.247Z
This is just a working example which shows how to parse existing blk-files and store data somewhere else
amaclin
CC BY-SA 3.0
2014-10-27T06:34:19.777Z
2014-10-27T06:34:19.777Z
amaclin
<p>I am working on a bitcoin research project. I want to decode receiving blocks from the block chain and store decoded data in a separate file for analyzing purposes.</p>

<p>For this purpose I digs the bitcoin c++ code and find the exact place, which use to write the blocks into <em>blkXXX.dat</em> file.</p>

<p>Could you please suggest a solution for this matter.</p>

<p>Thank you</p>

<bitcoind><c++>
Decode blocks received and Store in another file for analyzing
Rasela Don
# Decode blocks received and Store in another file for analyzing

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

I am working on a bitcoin research project. I want to decode receiving blocks from the block chain and store decoded data in a separate file for analyzing purposes.

For this purpose I digs the bitcoin c++ code and find the exact place, which use to write the blocks into *blkXXX.dat* file.

Could you please suggest a solution for this matter.

Thank you

**Tags:** bitcoind, c++


---

## 1 answers

### Answer by username_1 on 2014-10-27. Score: 2

```
#include <QTimer>
#include <QFile>

#include "BlockChain.h"

BlockChain::BlockChain ( const int start, QObject* parent ) : QFile ( parent ), blkFile ( start + 1 )
{
  QTimer::singleShot ( 0, this, SLOT ( start ( ) ) );
}
//--------------------------------------------------------------
void BlockChain::start ( )
{
  setFileName ( blkFileName ( blkFile++ ) );
  if ( !open ( QIODevice::ReadOnly ) )
  {
    _trace ( QString ( "cant open [%1]" ).arg ( fileName ( ) ) );
    getParent ( ).block ( QByteArray ( ) ); // finish signal
    deleteLater ( );
  }
  else
  {
    _trace ( QString ( "processing [%1]" ).arg ( fileName ( ) ) );
    QTimer::singleShot ( 0, this, SLOT ( next ( ) ) );
  }
}
//--------------------------------------------------------------
void BlockChain::next ( )
{
  bool lock ( true );
  if ( pos ( ) < size ( ) )
  {
    quint32 magic;
    quint32 size ( read ( (char*)&magic, 4 ) );
    xassert ( ( ( magic == MAGIC_ID ) || !magic ) && ( size == 4 ) );
    if ( magic )
    {
      read ( (char*)&size, 4 );
      xassert ( size > HEADER_SIZE && size <= MAX_BLOCK_SIZE );
      getParent ( ).block ( read ( size ) ); // process data
      QTimer::singleShot ( 0, this, SLOT ( next ( ) ) );
      return;
    }
    else
      lock = false;
  }
  close ( );
  getParent ( ).doneFile ( lock, blkFile - 1 );
  QTimer::singleShot ( 0, this, SLOT ( start ( ) ) );
}
//--------------------------------------------------------------
const QString BlockChain::blkFileName ( const int i )
{
  return
    ( i < 10 ) ? QString ( DATA_ROOT "\\blk0000%1.dat" ).arg ( i ) :
    ( i < 100 ) ? QString ( DATA_ROOT "\\blk000%1.dat" ).arg ( i ) :
    QString ( DATA_ROOT "\\blk00%1.dat" ).arg ( i );
}

```

> **Comments:**
> **username_0**: Thank you very much for take your time to reply me. Actually I am trying to find a way to do the enhancement in C++ core, not the qt-client. Because we dont use qt environment for development.  
> **username_1**: This is just a working example which shows how to parse existing blk-files and store data somewhere else  

---

