Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, let's do a hex dump to see what is really stored in the file.</p> <p><code>hexdump -C b.txt</code> or <code>od -t x2 -t c b.txt</code> are two examples (od is for octal dump, more common, less pretty output in my opinion) </p> <pre><code>00000000 34 20 35 0a |4 5.| 00000004 </code></pre> <p>That's is what the file looks like if it was a created as an ASCII text file (such as using a text editor like vi). You can use <code>man ascii</code> to double check the hexadecimal values.</p> <p>Now if you had a binary file that only contains two 8-bit bytes, in the system's native byte ordering (e.g. little-endian for x86, big endian for MIPS, PA-RISC, 680x0) then the hexdump would look like:</p> <pre><code>00000000 04 05 |..| 00000004 </code></pre> <p>Here is the code to both create (open &amp; write) a binary file, and read it back.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;stdint.h&gt; /* uint32_t */ #include &lt;sys/stat.h&gt; #include &lt;fcntl.h&gt; #include &lt;string.h&gt; #include &lt;errno.h&gt; /* User has read &amp; write perms, group and others have read permission */ const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; typedef struct prova { uint32_t first; uint32_t second; } prova_t; #define FILENAME "file.b" /* 'Safe' write */ int safewrite( int fd, const void *p, size_t want) { int ret; errno = 0; while (want) { ret = write(fd, (uint8_t *)p, want); if (ret &lt;= 0) { if (errno != EINTR &amp;&amp; errno != EAGAIN) { return -1; } errno = 0; continue; } want -= ret; p = (uint8_t*) p + ret; } return 0; } int saferead(int fd, const void *p, size_t want) { int ret; errno = 0; while (want) { ret = read(fd, (uint8_t*)p, want); if( ret == 0 ) return -1; /* EOF */ if (ret &lt;= 0) { if( errno != EINTR &amp;&amp; errno != EAGAIN ) { return -1; } errno = 0; continue; } want -= ret; p = (uint8_t*) p + ret; } return 0; } int main(int argc, char **argv) { int fd; prova_t result; size_t nbytes; /* Create file */ fd = creat(FILENAME, mode); if (fd &lt; 0) { fprintf(stderr, "Unable to open " FILENAME ": %s\n", strerror(errno)); exit(EXIT_FAILURE); } nbytes = sizeof(prova_t); result.first = 4; result.second = 5; if (0 != safewrite(fd, &amp;result, nbytes)) { fprintf(stderr, "Unable to write to " FILENAME ": %s\n", strerror(errno)); exit(EXIT_FAILURE); } close(fd); fd = -1; /* Reopen and read from binary file */ fd = open(FILENAME, O_RDONLY); nbytes = sizeof(prova_t); if (0 != saferead(fd, &amp;result, nbytes)) { fprintf(stderr, "Unable to read file \"" FILENAME "\": %s\n", strerror(errno)); exit(EXIT_FAILURE); } close(fd); printf( "Read: %d %d (%#.02x%.02x)\n", result.first, result.second, result.first, result.second); return EXIT_SUCCESS; } </code></pre> <p>Now the data file contents look like:</p> <pre><code>00000000 04 00 00 00 05 00 00 00 |........| 00000008 </code></pre> <p>Because the integers were specified as 32-bit integers (32-bits / 8 bits per byte = 4 bytes). I'm using a 64-bit system (little endian, x86), so I wanted to be explicit so the your results should match, assuming little-endian.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload