title: Why doesn't io:write() write to the output file?
body: <p>I'm writing a short script in Lua to replicate Search/Replace functionality. The goal is to enter a search term and a replacement term, and it will comb through all the files of a given extension (not input-determined yet) and replace the Search term with the Replacement term. </p>
  
  <p>Everything seems to do what it's supposed to, except the files are not actually written to. My Lua interpreter (compiled by myself in Pelles-C) does not throw any errors or exit abnormally; the script completes as if it worked. </p>
  
  <p>At first I didn't have i:flush(), but I added it after reading that it is supposed to save any written data to the file (see <a href="http://www.lua.org/manual/5.2/manual.html#pdf-io.flush" rel="nofollow noreferrer">LUA docs</a>). It didn't change anything, and files are still not written to.</p>
  
  <p>I <em>think</em> it might have something to do with how I'm opening the file to edit it, since the "w" option works (but overwrites everything in my test files).</p>
  
  <p><strong>Source:</strong></p>
  
  <pre><code>io.write("Enter your search term:")
  term = io.read()
  
  io.write("Enter your replace term:")
  replacement = io.read()
  
  io.stdin:read()
  
  t = {}
  for z in io.popen('dir /b /a-d'):lines() do
  
      if string.match(string.lower(z), "%.txt$")  then
          print(z)
          table.insert(t, z)
      end
  end
  print("Second loop")
  
  for _, w in pairs(t) do
      print(w)
      i = io.open(w, "r+")
      print(i)
  
      --i:seek("set", 6)
      --i:write("cheese")
      --i:flush()
      for y in i:lines() do
          print(y)
          p, count = string.gsub(y, term, replacement, 1)
          print(p)
  
          i:write(p)
  
          i:flush()
          io.stdin:read()
      end
      i:close()
  end
  </code></pre>
  
  <p>This is the output I get (which is what I <em>want</em> to happen), but in reality isn't being written to the file:</p>
  
  <p><img src="https://i.stack.imgur.com/IzxeS.png" alt="Misleading output"></p>
  
  <p>There was one time where it wrote output to a file, but it only output to one file and after that write my script crashed with the message: <code>No error</code>. The line number was at the <code>for y in i:lines() do</code> line, but I don't know why it broke there. I've noticed <code>file:lines()</code> will break if the file itself has nothing in it and give an odd/gibberish error, but there are things in my text files.</p>
  
  <p><strong>Edit1</strong></p>
  
  <p>I tried do this in my for loop:</p>
  
  <pre><code>for y in i:lines() do
          print(y)
          p, count = string.gsub(y, term, replacement, 1)
          print(p)
  
          i:write(p)
          i:seek("set", 3)        --New
          i:write("TESTESTTEST")  --New
          i:flush()
          io.stdin:read()
  end
  </code></pre>
  
  <p>in order to see if I could force it to write regular text. It <em>does</em> but then it crashes with <code>No error</code> and still doesn't write the replacement string (just TESTESTTEST). I don't know what the problem could be.</p>