[Solved]How do I print a string to txt file?

Started by zutokaza, November 24, 2016, 09:02:08 PM

Previous topic - Next topic
November 24, 2016, 09:02:08 PM Last Edit: April 10, 2017, 08:23:30 AM by zutokaza
Hello,

I want to print a string to a text file.

For example,

Code ( lua) Select
print("hello world")

txt file:
1 hello world
Solarus Works on ReactOS Opensource Windows OS

https://www.reactos.org/forum/viewtopic.php?f=2&t=14759&p=120616#p120616

Use the Lua standard library: io.open and then file:write().
print() is just for debugging.

If you want to write the somewhere relative to the quest directory, then you may want to use sol.file.open() instead of io.open(). By default it writes the file to the same location as your quest save data. This is probably what you'd want if you are writing supplemental data beyond what gets written to the save file.

If you are writing some sort of data log for debugging purposes, then you'd probably want to use io.open(), where you'd specify a full file path to somewhere on your local machine.

Also, be sure to read the documentation for io.open(), as you may want a write mode other than "w" depending on your usage.

Example:
Code (lua) Select

local file = sol.file.open("MyFile.txt", "w")
file:write("Hello World")
file:close()


Quotefile:write("Hello World")
Can I write the value of a variable?

For example,
Code ( lua) Select
local coordinate_x = 50
local coordinate_y = 40
file:write("x:",coordinate_x, "y:", coordinate_y)
Solarus Works on ReactOS Opensource Windows OS

https://www.reactos.org/forum/viewtopic.php?f=2&t=14759&p=120616#p120616

Quote from: zutokaza on November 25, 2016, 05:53:18 AM
Can I write the value of a variable?

You can write any string. How your script interprets the string is up to you.

The example you gave works, but if you are just writing simple integer values, you are probably better off saving the values to your savegame data like so:
Code (lua) Select

game:set_value("coordinate_x", 50)
game:set_value("coordinate_y", 40)


And to read it back:
Code (lua) Select

game:get_value("coordinate_x")
game:get_value("coordinate_y")


April 08, 2017, 01:26:38 AM #6 Last Edit: April 17, 2017, 02:25:10 AM by Zefk
This might be what you want. Create your text file manually just in case. The file is "test.txt" in the usage example below. I got an error until I created it.


Read Line Function:
Code ( lua) Select
--Read line function
local function readLines(sPath)
  local file = sol.file.open(sPath, "r")
  if file then
        local tLines = {}
        local sLine = file:read()
        while sLine do
          table.insert(tLines, sLine)
          sLine = file:read()
        end
        file:close()
        return tLines
  end
  return nil
end


Write Line Function:
Code ( lua) Select
--Write line function
local function writeLines(sPath, tLines)
  local file = sol.file.open(sPath, "w")
  if file then
        for _, sLine in ipairs(tLines) do
          file:write(sLine)
        end
        file:close()
  end
end


Usage:

Code ( lua) Select
--Make a text file
local file_make_test = sol.file.open("test.txt", "w")
file_make_test:close()

local tLines = readLines("test.txt") -- Read this file
table.insert(tLines, "This is the first line!\n") -- Line 1
tLines[2] = "This is line 2!\n" -- Line 2
tLines[3] = "This is line 3!\n" -- Line 3
tLines[4] = 50 -- Line 4

table.remove(tLines, 2) -- Remove line 2
writeLines("test.txt", tLines) --Write lines to this file
print("Lines in the file: ", #tLines) --Print number of lines

--Open file. You must open the file to get the value
local tLines = readLines("test.txt") -- Read this file

--Print line 3. Line 4 will not be 50 because we removed line 2. That means line 3 will be 50.
print("Line 4 value is: "..tLines[3])

April 08, 2017, 01:36:44 AM #7 Last Edit: April 10, 2017, 08:51:36 PM by zutokaza
That is exactly what I wanted. Thank you Zefk!
Solarus Works on ReactOS Opensource Windows OS

https://www.reactos.org/forum/viewtopic.php?f=2&t=14759&p=120616#p120616