This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ComputerCraftFan11's profile picture

Multiline file.write

Started by ComputerCraftFan11, 06 April 2012 - 07:19 AM
ComputerCraftFan11 #1
Posted 06 April 2012 - 09:19 AM
I'm making a mod maker and when I make a new file, everything is in 1 line

local path = "mcp/src/minecraft/net/minecraft/src";
local modname = ""
local start = [[
package net.minecraft.src;

public class mod_jBox extends BaseMod
{
]]
function top()
shell.run("clear")
print("Welcome to xXm0dzXx's Mod Maker!")
print("--------------------------------")
end
function blockmaker()
top()
end
function base()
top()
write("Enter mod name: mod_")
input = read()
modname = "mod_" ..input.. ".java"
if fs.exists(path.."/"..modname) then
  print(modname.. " already exists! Aborting!")
else
  file = io.open(path.."/"..modname, "w")
  file:write(start)
  file:close()
  print(modname.. " created!")
  sleep(1)
  menu()
end
end
function menu()
top()
print("[1] Create Base Files")
if modname == "" then
  print("[2] Make New Block (CREATE BASE FILES FIRST)")
  print("[X] Patch Mod (CREATE BASE FILES FIRST)")
else
  print("[2] Make New Block")
  print("[X] Patch Mod (Run to make the mod work)")
end
print("")
write("Selection: ")
input = read()
if input == "1" then
  base()
elseif input == "2" then
  if modname == "" then
   menu()
  else
   blockmaker()
  end
elseif input == "X" then
  if modname == "" then
   menu()
  else
   file = io.open(path.."/"..modname, "w")
   file:write(start)
   file:close()
  end
else
  menu()
end
end

if fs.exists("mcp/src") then
menu()
else
print("MCP not installed! Pleased decompile MCP and put it in the mcp folder.")
end
I tried putting a \n after each line in start = [[ but it didn't work
toxicwolf #2
Posted 06 April 2012 - 10:36 AM
I wrote these functions a little while back. They store each line individually in separate table indexes.

This function can be used to read a specific line from the given filepath:

function readFile(sPath, nLineNum)
local tFile = {}
local hHandle = fs.open(sPath, "r")
repeat
local sLine = hHandle:readLine()
table.insert(tFile, sLine)
until not sLine
hHandle:close()
return tFile[nLineNum]
end

And you can use this function to write a string to a specific line of a given filepath:

function writeFile(sText, sPath, nLineNum)
local tFile = {}
local hHandle = fs.open(sPath, "r")
repeat
local sLine = hHandle:readLine()
table.insert(tFile, sLine)
until not sLine
hHandle:close()
tFile[nLineNum] = sText
local hHandle = fs.open(sPath, "w")
for tNum = 0, #tFile do
hHandle.writeLine(tFile[tNum])
end
hHandle:close()
return
end

Hope this helps :)/>/>
Also note that this was written off the top of my head without testing in game, so it may not be 100% correct.

EDIT: Also, if you open the file using fs.open() as opposed to io.open(), you can use the file.writeLine() syntax, with file being whatever you named the file handle when you opened it.
Edited on 06 April 2012 - 08:42 AM
Mads #3
Posted 06 April 2012 - 05:04 PM
Nvm, I see you found out
Steelsouls #4
Posted 05 September 2012 - 06:51 PM
SpoilerI wrote these functions a little while back. They store each line individually in separate table indexes.

This function can be used to read a specific line from the given filepath:

function readFile(sPath, nLineNum)
local tFile = {}
local hHandle = fs.open(sPath, "r")
repeat
local sLine = hHandle:readLine()
table.insert(tFile, sLine)
until not sLine
hHandle:close()
return tFile[nLineNum]
end

And you can use this function to write a string to a specific line of a given filepath:

function writeFile(sText, sPath, nLineNum)
local tFile = {}
local hHandle = fs.open(sPath, "r")
repeat
local sLine = hHandle:readLine()
table.insert(tFile, sLine)
until not sLine
hHandle:close()
tFile[nLineNum] = sText
local hHandle = fs.open(sPath, "w")
for tNum = 0, #tFile do
hHandle.writeLine(tFile[tNum])
end
hHandle:close()
return
end

Hope this helps :D/>/>
Also note that this was written off the top of my head without testing in game, so it may not be 100% correct.

EDIT: Also, if you open the file using fs.open() as opposed to io.open(), you can use the file.writeLine() syntax, with file being whatever you named the file handle when you opened it.

Hey toxicwolf, thanks for posting this, it's helped me a lot with my turtle mining program. Now I can have it continue where I left off. I have a question about the write function. Is it necessary in Lua to have return within a function if you're not actually returning anything? That write function you made would end on its own and doesn't return any values, so what does adding a blank return in there do? Thanks in advance for any insight you can give.
Pharap #5
Posted 06 September 2012 - 04:08 AM
is this supposed to be Java or lua? last time I checked lua didn't have a package or extends word, yet everything else seems like lua.
kazagistar #6
Posted 06 September 2012 - 08:15 PM
is this supposed to be Java or lua? last time I checked lua didn't have a package or extends word, yet everything else seems like lua.
Its a mod maker, he is creating a multiline string using [[ ]] that contains java code and then writing it to a file.

Basically, lua that writes java.
Pharap #7
Posted 06 September 2012 - 09:00 PM
is this supposed to be Java or lua? last time I checked lua didn't have a package or extends word, yet everything else seems like lua.
Its a mod maker, he is creating a multiline string using [[ ]] that contains java code and then writing it to a file.

Basically, lua that writes java.

I see now, I missed the [[ and ]]. Probably down to the way I tend to format my lua code.
It's a good idea at least. I intend to learn how to mod someday but there's so much minecraft code I'd need a manual to explain how it all runs to me. It's not that I don't know how to program, it's just their naming scheme and how cluttered it all is.
DisFunqTional #8
Posted 09 September 2012 - 04:20 PM
file.writeLine() adds the n just like print() with the console