Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The code is not 100% portable if you are writing memory contents to files.</p> <p>You need something called <strong>serialization</strong>. Ok, computer science term, but it basically means that you get your data and transform it into a well-defined and documented sequence of bytes, which can be read back to memory later by the same or another program. This sequence of bytes is architecture and platform-independent.</p> <p>Most Unix environments already come with a <a href="http://linux.die.net/man/3/xdr" rel="nofollow noreferrer">XDR</a> implementation, which provides routines for data serialization.</p> <p>A simple example encoding 4 doubles to stdout (you can use shell redirection, or use fopen() to open a file instead of stdout):</p> <pre><code>XDR xdrs; double data[4] = { 1.0, 255.41, -357.1, 123.4 }; int i; xdrstdio_create(&amp;xdrs, stdout, XDR_ENCODE); for (i = 0; i &lt; 4; i++) xdr_double(&amp;xdrs, &amp;data[i]); </code></pre> <p>Now, to get these doubles back (from stdin) and print them:</p> <pre><code>XDR xdrs; double data; int i; xdrstdio_create(&amp;xdrs, stdin, XDR_DECODE); for (i = 0; i &lt; 4; i++) { xdr_double(&amp;xdrs, &amp;data); printf("%g\n", data); } </code></pre> <p>You can encode and decode complex structures using XDR. This was a very dumb way of sending four doubles to a file, and generally you should instead use xdr_array() to read/write arrays of some data type. The same commands, in the same order, have to be executed when saving and when loading the file. In fact, you can use <a href="http://linux.die.net/man/1/rpcgen" rel="nofollow noreferrer">rpcgen</a> to generate C structs and their corresponding xdr functions automatically.</p>
 

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