alternative reading binary in python instead of C++
<p>I have a binary file and C++ code which can read that binary file like follow.</p>

<pre><code>int NumberOfWord;
FILE *f = fopen("../data/vec.bin", "rb");
fscanf(f, "%d", &amp;NumberOfWord);
cout &lt;&lt; NumberOfWord&lt; &lt;endl;
</code></pre>

<p>This output is:</p>

<pre><code>114042
</code></pre>

<p>I want to reimplement like above code in python.</p>

<pre><code>with open("../data/vec.bin","rb") as f:
    b = f.read(8)
    print struct.unpack("d",b)[0]
</code></pre>

<p>but this code is not working.
my output is:</p>

<pre><code>8.45476330511e-53
</code></pre>

<p>My question are:</p>

<p>1) why integer has 8 byte in C++.</p>

<p>I never knows %d means double.
but, actually the variable has a type of integer, but normally we output using "%d" in C++.
It is weird.</p>

<p>2) How do I extract a real number in python</p>

<p>I want to extract a real number like above C++ code in python code.
How do I that??</p>

<p>maybe, I misunderstand about struct module in python.</p>

<p>In C format strings, <code>%d</code> is short for decimal.</p>

<p>In Python, <code>d</code> is short for double.</p>

<p>If it's an integer, you should use <code>i</code> in <code>struct.unpack</code> call.</p>

<pre><code>with open("../data/vec.bin","rb") as f:
    b = f.read()
    print struct.unpack("i",b)[0]
</code></pre>

python c++