Author Topic: [Solved]How do I print a string to txt file?  (Read 7710 times)

0 Members and 1 Guest are viewing this topic.

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
[Solved]How do I print a string to txt file?
« on: November 24, 2016, 09:02:08 PM »
Hello,

I want to print a string to a text file.

For example,

Code: ( lua) [Select]
print("hello world")
txt file:
1 hello world
« Last Edit: April 10, 2017, 08:23:30 AM by zutokaza »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1195
    • View Profile
Re: How do I print a string to txt file?
« Reply #1 on: November 24, 2016, 10:11:43 PM »
Use the Lua standard library: io.open and then file:write().
print() is just for debugging.

llamazing

  • Full Member
  • ***
  • Posts: 221
    • View Profile
Re: How do I print a string to txt file?
« Reply #2 on: November 25, 2016, 01:17:53 AM »
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()

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
Re: How do I print a string to txt file?
« Reply #3 on: November 25, 2016, 05:53:18 AM »
Quote
file: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)

llamazing

  • Full Member
  • ***
  • Posts: 221
    • View Profile
Re: How do I print a string to txt file?
« Reply #4 on: November 25, 2016, 06:03:22 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")

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
How to read a specific line in a text file?
« Reply #5 on: April 07, 2017, 08:41:13 AM »
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

Zefk

  • Hero Member
  • *****
  • Posts: 535
  • Just helping Solarus
    • View Profile
    • Zefk Design
Re: How do I print a string to txt file?
« Reply #6 on: April 08, 2017, 01:26:38 AM »
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])

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
[Solved]How do I print a string to txt file?
« Reply #7 on: April 08, 2017, 01:36:44 AM »
That is exactly what I wanted. Thank you Zefk!
« Last Edit: April 10, 2017, 08:51:36 PM by zutokaza »