Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your question isn't very specific, so I can only recommend some general ideas.</p> <p>You might like to read the file in chunks, for example 4096 bytes at a time (that's the typical page size) -- though larger chunks should also be fine (maybe 64kiB or 512kiB even, just experiment). Once you got a chunk read, process it from memory.</p> <p>To be correct, we should generate the chunk memory as an array of the target integer. For example, for 4-byte integers we could do this:</p> <pre><code>#include &lt;cstdint&gt; #include &lt;memory&gt; #include &lt;cstdio&gt; uint32_t buf[1024]; typedef std::unique_ptr&lt;std::FILE, int (*)(std::FILE *)&gt; unique_file_ptr; static unique_file_ptr make_file(const char * filename, const char * flags) { std::FILE * const fp = std::fopen(filename, flags); return unique_file_ptr(fp ? fp : nullptr, std::fclose); } int main() { auto fp = make_file("thedata.bin", "rb"); if (!fp) return 1; while (true) { if (4096 != std::fread(reinterpret_cast&lt;char*&gt;(buf), 4096, fp.get())) break; // process buf[0] up to buf[1023] } } </code></pre> <p>I chose the C-library <code>fopen</code>/<code>fread</code> over C++ iostreams for performance reasons; I cannot actually claim that that decision is based on personal experience. (If you have an old compiler, you might need the header <code>&lt;stdint.h&gt;</code> instead, and perhaps you don't have <code>unique_ptr</code>, in which case you cah just use <code>std::FILE*</code> and <code>std::fopen</code> manually.)</p> <p>Alternative to the global <code>buf</code>, you could also make an <code>std::vector&lt;uint32_t&gt;</code>, resize it to something large enough and read into its data buffer directly (<code>&amp;buf[0]</code> or <code>buf.data()</code>).</p> <p>If you need to read integers that are not 2, 4, 8 or 16 bytes long, you'll have to read into a char array and extract the numbers manually with algebraic operations (e.g. <code>buf[pos] + (buf[pos + 1] &lt;&lt; 8) + (buf[pos + 2] &lt;&lt; 16)</code> for a 3-byte integer). If your packing isn't even byte-aligned, you'll have to make an even greater effort.</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. This table or related slice is empty.
    1. This table or related slice is empty.
    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