Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here are some of the errors in your algorithm.</p> <ul> <li><p>You read <strong>and look at</strong> one BUFFER of chars at a time, with no overlap. What if <code>str</code> appears between buffers? (i.e. the first part of <code>str</code> is at the end of a buffer and the second part is at the start of the next buffer).</p></li> <li><p>You try to overwrite <code>str</code> with <code>replace</code> directly in the buffer using <code>strcpy</code>. What if both strings are of different length? If <code>replace</code> is shorter than <code>str</code>, you'd still have the end of <code>str</code> there and if <code>replace</code> is longer, it will overwrite the text following <code>str</code></p></li> <li><p>Even if they are the same length, <code>strcpy</code> adds the final 0 char at the end of the copy (that's how they tell you where the string ended). you DEFINITIVELY don't want that. Maybe <code>strncpy</code> is a better suggestion here, although it will still not work if both strings aren't the same length.</p></li> <li><p>You replace the strings in the buffer but do nothing with the "corrected" buffer! The buffer is not the file, the content of the file was COPIED into the buffer. So you changed the copy and then nothing. The file will not change. You need to write your changes into a file, preferably a different one.</p></li> </ul> <p>Writing such a replace isn't as trivial as you might think. I may try and help you, but it might be a bit over your head if you're just trying to learn working with files and are still not fully comfortable with strings.</p> <p>Doing the replace in a single file is easy if you have enough memory to read the entire file at once (if <code>BUFFER</code> is larger than the file size), but very tricky if not especially in your case where <code>replace</code> is longer than <code>str</code>.</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