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

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
  1. print("hello world")

txt file:
1 hello world
« Last Edit: April 10, 2017, 08:23:30 am by zutokaza »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1041
    • 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

  • Jr. Member
  • **
  • Posts: 73
    • 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
  1. local file = sol.file.open("MyFile.txt", "w")
  2. file:write("Hello World")
  3. 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
  1. local coordinate_x = 50
  2. local coordinate_y = 40
  3. file:write("x:",coordinate_x, "y:", coordinate_y)

llamazing

  • Jr. Member
  • **
  • Posts: 73
    • 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
  1. game:set_value("coordinate_x", 50)
  2. game:set_value("coordinate_y", 40)

And to read it back:
Code: Lua
  1. game:get_value("coordinate_x")
  2. 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

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
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
  1. --Read line function
  2. local function readLines(sPath)
  3.   local file = sol.file.open(sPath, "r")
  4.   if file then
  5.         local tLines = {}
  6.         local sLine = file:read()
  7.         while sLine do
  8.           table.insert(tLines, sLine)
  9.           sLine = file:read()
  10.         end
  11.         file:close()
  12.         return tLines
  13.   end
  14.   return nil
  15. end
  16.  

Write Line Function:
Code: Lua
  1. --Write line function
  2. local function writeLines(sPath, tLines)
  3.   local file = sol.file.open(sPath, "w")
  4.   if file then
  5.         for _, sLine in ipairs(tLines) do
  6.           file:write(sLine)
  7.         end
  8.         file:close()
  9.   end
  10. end

Usage:

Code: Lua
  1. --Make a text file
  2. local file_make_test = sol.file.open("test.txt", "w")
  3. file_make_test:close()
  4.  
  5. local tLines = readLines("test.txt") -- Read this file
  6. table.insert(tLines, "This is the first line!\n") -- Line 1
  7. tLines[2] = "This is line 2!\n" -- Line 2
  8. tLines[3] = "This is line 3!\n" -- Line 3
  9. tLines[4] = 50 -- Line 4
  10.  
  11. table.remove(tLines, 2) -- Remove line 2
  12. writeLines("test.txt", tLines) --Write lines to this file
  13. print("Lines in the file: ", #tLines) --Print number of lines
  14.  
  15. --Open file. You must open the file to get the value
  16. local tLines = readLines("test.txt") -- Read this file
  17.  
  18. --Print line 3. Line 4 will not be 50 because we removed line 2. That means line 3 will be 50.
  19. print("Line 4 value is: "..tLines[3])
« Last Edit: April 17, 2017, 02:25:10 am by Zefk »

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 »