reading a binary file with mmap()
<p>Could you please explain why my program reads strange values from a file? I'm trying to read a file using mmap. The reason I use this function is to understand how it works. Then I'd use it with /dev/mem in order to read binary data at specific physical memory address.   </p>

<p>I made a file 'hello1.raw' using the following python script. </p>

<pre><code>fd = open("hello1.raw", "w+b")
fd.write(b'01')

fd.close()
</code></pre>

<p>Then I tried to read it using the c program below.</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;sys/mman.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;

int main(){

int fd = open("hello1.raw",O_RDWR);

struct stat st;
fstat(fd, &amp;st);
size_t size=st.st_size;

int * a = mmap(0, size, PROT_READ|PROT_EXEC ,MAP_SHARED , fd, 0);
printf("address of hello1.raw: %p\n",a); 
printf("data in hello1.raw: %d\n", *a);

int b = munmap(a, size);
close(fd);

return 0;
}
</code></pre>

<p>But this program prints weird value. </p>

<pre><code>address of hello.raw: 0x10238d000
data in hello.raw: 12592
</code></pre>

<p>The data is not 01 but 12592.  </p>