Note that there are some explanatory texts on larger screens.

plurals
  1. POBug in my 'next offset' algorithm - C++
    primarykey
    data
    text
    <p>I am trying to apply filenames to the files loaded from an archive. </p> <p>For the first step, I create an array of structs that hold three bits of information. The filename, the file size and the offset in the file (from the beginning). The file sizes and offsets are initially read in to the structs. Then, I decode the filenames and read them into a vector.</p> <p>Here is where it gets tricky. The filenames in order apply to the order of the offsets. The offsets however are not in order. For example:</p> <pre><code>File 1: Name: Size: 20102 Offset: 16 File 2: Name: Size: 23419 Offset: 2040 File 3: Name: Size: 145 Offset: 350 </code></pre> <p>So with 3 filenames decoded into my vector, I would apply filename #1 to file 1, filename #2 to file 3 (because it has a lower offset), and finally filename #3 to file 2.</p> <p>Now, my algorithm for doing this doesn't seem to work correctly. Here is the code:</p> <pre><code>// Keep track of the last offset we used int last_offset = 0; int current_offset = 0; int index = 0; // Finally, apply the correct filenames to the correct files for(int i = 0; i &lt; file_names.size(); i++) { for(int j = 0; j &lt; file_count - 1; j++) { if(j == 0) { current_offset = files[j].offset; } if(files[j].offset &gt; last_offset &amp;&amp; files[j].offset &lt; current_offset) { index = j; current_offset = files[j].offset; } } files[index].name = file_names[i]; last_offset = current_offset; } </code></pre> <p>file_names is my vector with the strings in it. file_count is subtracted by 1 because it includes the directory file for the archive which does not need to be counted. It is also the highest offset and therefore not even dealt with.</p> <p>Basically, this code tries to find the next highest offset every time and then apply the next file name in the vector.</p> <p>Here is the output from my error log:</p> <pre><code>&lt;-!-&gt; File debug name: &lt;-!-&gt; File size: 17464 &lt;-!-&gt; File offset: 47974 &lt;-!-&gt; File debug name: 1dirt.bmp &lt;-!-&gt; File size: 17462 &lt;-!-&gt; File offset: 12 &lt;-!-&gt; File debug name: rrock.bmp &lt;-!-&gt; File size: 17464 &lt;-!-&gt; File offset: 17011 &lt;-!-&gt; File debug name: mtfloor.bmp &lt;-!-&gt; File size: 5176 &lt;-!-&gt; File offset: 30725 &lt;-!-&gt; File debug name: &lt;-!-&gt; File size: 17640 &lt;-!-&gt; File offset: 134953 &lt;-!-&gt; File debug name: &lt;-!-&gt; File size: 158 &lt;-!-&gt; File offset: 140286 &lt;-!-&gt; File debug name: &lt;-!-&gt; File size: 134188 &lt;-!-&gt; File offset: 81658 &lt;-!-&gt; File debug name: lights.wld &lt;-!-&gt; File size: 17464 &lt;-!-&gt; File offset: 34273 &lt;-!-&gt; File debug name: &lt;-!-&gt; File size: 1496 &lt;-!-&gt; File offset: 139799 &lt;-!-&gt; File debug name: &lt;-!-&gt; File size: 17464 &lt;-!-&gt; File offset: 61625 </code></pre> <p>This indicates it has clearly not worked.</p> <p>The other option I can do is <strong>sort the array of structs by offsets ascending,</strong> which I have no clue how to do but would make this process much easier as I could just apply the names in order.</p> <p>Thank you for your time and let me know if you see the error.</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.
 

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