title: Lua - Trying to write to a file
body: <p>I'm using Rainmeter to pass air pressure values to a lua script which I then want to be written to a text file and store the last x number to be retrieved later on to produce a histogram of the pressure changes over time.</p>
  
  <p>To start with I am just trying to write a string to a file as a test but I am failing and feeling quite stupid in the process.</p>
  
  <p>My script has an Update function where I'm passing and returning the pressure value from Rainmeter then another function called writetofile where I'm trying to open, write and then close a text file. It is this part I am having trouble with.</p>
  
  <p>My script is as follows:</p>
  
  <pre><code>function Initialize()
  end -- function Initialize
  
  function Update()
      sMyPressure = SELF:GetOption('CurrentPressure')
      return sMyPressure
  end -- function Update
  
  function writetofile()
      file = io.open("pressures.txt", "w")
      file:write("Hello World")
      file:close()
  end
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I tried removing the writetofile function and moving the contents to within the update function but this didn't work and the script stopped passing the sMyPressure back to the Rainmeter script and also didn't write to the text file.</p>
  
  <p>eg:</p>
  
  <pre><code>function Initialize()
  end -- function Initialize
  
  function Update()
      sMyPressure = SELF:GetOption('CurrentPressure')
      return sMyPressure
      file = io.open("pressures.txt", "w")
      file:write("Hello World")
      file:close()
  end -- function Update
  </code></pre>
  
  <p>What am I doing wrong? the file pressures.txt exists and is writable and if I delete it, it isn't created.</p>
  
  <p>Can anyone help please.</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Got it working.</p>
  
  <p>1) I can't call a function from within Update</p>
  
  <p>2) As Mike said, I needed a full path to the file</p>
  
  <p>3) This path requires double backspaces \</p>
  
  <p>Working code if anyone is interested or has the same issue:</p>
  
  <pre><code>function Initialize()
  end -- function Initialize
  
  function Update()
      sMyPressure = SELF:GetOption('CurrentPressure')
      writetofile(sMyPressure)
      return sMyPressure
  end -- function Update
  
  function writetofile(CurrentPressure)
      file = io.open("C:\\Users\\Hedley\\Documents\\Rainmeter\\Skins\\SimplyNova\\Wunderground\\pressures.txt", "a")
      -- file:write("hello", "\n")
      file:write(CurrentPressure, "\n")
      file:close()
  end
  </code></pre>