Hello,
I want to print a string to a text file.
For example,
print("hello world")
txt file:
1 hello world
Use the Lua standard library: io.open (https://www.lua.org/manual/5.1/manual.html#pdf-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:
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,
local coordinate_x = 50
local coordinate_y = 40
file:write("x:",coordinate_x, "y:", coordinate_y)
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:
game:set_value("coordinate_x", 50)
game:set_value("coordinate_y", 40)
And to read it back:
game:get_value("coordinate_x")
game:get_value("coordinate_y")
I was wondering if there was a way to read specific lines and remove them if necessary.
Like this:
http://www.computercraft.info/forums2/index.php?/topic/2886-luahow-to-readwrite-certain-lines-in-files/page__view__findpost__p__21538
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:
--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:
--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:
--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])
That is exactly what I wanted. Thank you Zefk!