Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Note that you've provided a mapping for the file that is exactly the size of the file. If you create the file in your call to <code>open(2)</code>, it will have a length of <code>0</code>, and I wouldn't be surprised if the kernel doesn't bother setting up any kind of memory mapping from a <code>0</code> length mapping. (Maybe it does? I've never tried...)</p> <p>I would suggest using <code>ftruncate(2)</code> to extend the length of your file <em>before</em> performing the mapping. (Note that extending files using <code>ftruncate(2)</code> isn't very portable; not all platforms provide extending functionality and not all filesystem drivers support the extending functionality. See your system's manpage for details.)</p> <p>You must use the <code>MAP_SHARED</code> mapping for your file modifications to be saved to disk.</p> <p>Your use of <code>perror(3)</code> isn't quite correct; <code>perror(3)</code> will <em>not</em> terminate your program, so it will continue executing with incorrect assumptions:</p> <pre><code>if((ret=fstat(fd,&amp;st)) &lt; 0) perror("Error in fstat"); </code></pre> <p>Should read:</p> <pre><code>if((ret=fstat(fd,&amp;st)) &lt; 0) { perror("Error in fstat"); exit(1); } </code></pre> <p>(Or <code>exit(EXIT_FAILURE)</code> if you want to be more portable -- I find that a little harder on the eyes but I live in Linux-land.)</p> <p><code>strcat(3)</code> expects to find an ASCII NUL character (byte value <code>0x00</code>, C representation <code>'\0'</code>) -- the usual C end-of-string marker -- at the end of the <code>dest</code> string. Your file will not contain an ASCII NUL if you create it in this program -- its length is zero, after all -- and I don't know the consequences of trying to read a zero-byte file via <code>mmap(2)</code>. If the file already exists and has data in it, it <em>probably</em> doesn't have an ASCII NUL encoded in the file. <code>strcat(3)</code> is almost certainly the wrong tool to write into your file. (No one wants ASCII NULs in their files anyway.) Try <code>memcpy(3)</code> instead.</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