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

How to make a text input to a file?

Started by IsaacTBeast, 29 May 2014 - 03:59 AM
IsaacTBeast #1
Posted 29 May 2014 - 05:59 AM
Hey guys! This is my logic and I will post my code in too :)/>

this is what I want too do..


usage: input -i [(Text)] [(File Name)]

the command makes a file and then it inputes the Text what you typed..
CCJJSax #2
Posted 29 May 2014 - 07:03 AM
You can use the fs api.


This code is by theoriginalbit that is cleanup of mysticT's code. (I haven't tested this one yet, but I have tested mysticT's and it worked amazingly)

local function readLines(sPath)
  local fHandle = fs.open(sPath, 'r')
  if fHandle then
	local tLines = {}
	for sLine in fHandle.readLine do
	  tLines[#tLines+1] = sLine
	end
	fHandle.close()
	return tLines
  end
end
local function writeLines(sPath, tLines)
  local fHandle = fs.open(sPath, 'w')
  if fHandle then
	fHandle.writeLine(table.concat(tLines, '\n'))
	fHandle.close()
  end
end
I believe this should create the file if it's not there.


to be more clear, I'll show you an example usage (untested). If it's off sorry, I'll test it soon, or someone will tell us it's wrong. for now I'm tired.

fileContent = readLines("file")
-- this will be a table including all the lines from the file.  Ignore it if you dont need to get information from the file.

writeContent = {[1] = "hello" [2] = "world"}
writeLines("file", writeContent)
Edited on 29 May 2014 - 05:10 AM
IsaacTBeast #3
Posted 29 May 2014 - 07:29 AM
it doesn't work :/


function readLines(sPath)
  local fHandle = fs.open(sPath, 'r')
  if fHandle then
    local tLines = {}
    for sLine in fHandle.readLine do
	  tLines[#tLines+1] = sLine
    end
    fHandle.close()
    return tLines
  end
end
function writeLines(sPath, tLines)
  local fHandle = fs.open(sPath, 'w')
  if fHandle then
    fHandle.writeLine(table.concat(tLines, '\n'))
    fHandle.close()
  end
end
function printUsage()
   print("Usage: file [(OPTION)] [(File Name)]")
   print("Usage: file -i [(File Name)] [(Text)]")
   print(" ")
   print("-find    -- Is an option that finds a file.")
   print(" ")
   print("-o	 -- It shows the output of your file content.")
   print("-i	 -- Write a Text to a made file.")
end
-- This program was created by IsaacTBest.
-- License: GNU/GPLv3 License.
-- Read GNU/GPLv3 License to know more about the laws.

local tArgs = { ... }
if #tArgs < 1 then
   printUsage()
end
local sCommand = tArgs[1]
if sCommand == "-find" then
local sPath = shell.resolve( tArgs[2] )
local tFiles = fs.find( sPath )
if #tFiles > 0 then
   for n,sFile in ipairs( tFiles ) do
	   if fs.exists( sFile ) then
		  print("File Exists!");
		  else
			 term.setTextColor(colors.red)
			 printError( "File does not exist!" )
	   end
   end
else
    printUsage()
end
end
if sCommand == "-o" then
  local sPath = shell.resolve( tArgs[2] )
  local tFiles = fs.find( sPath )
  if #tFiles > 0 then
	 for n,sFile in ipairs( tFiles ) do
	   if fs.exists( sFile ) then
		 local file = fs.open( sFile,"r" )
		 print("Title of file: "..sFile)
		 print("Content:")
		 print(file.readAll( sFile ))
		 file.close()
	   else
		 term.setTextColor(colors.red)
		 print( "File does not exist!" )
	   end
    end
  end
end
if sCommand == "-i" then
  local sPath = shell.resolve( tArgs[2] )
  local tFiles = fs.find( sPath )
  local tLines = shell.resolve ( tArgs[3] )
  if #tFiles > 0 then
	  for n,sFile in ipairs( tFiles ) do
		 writeLines(sFile, tLines)	   
	   end
    end
  end

This is what I put.
CCJJSax #4
Posted 29 May 2014 - 07:45 AM
I'm not sure why it didn't work for you. Here is what I did and it worked for me


local function readLines(sPath)
  local fHandle = fs.open(sPath, 'r')
  if fHandle then
	    local tLines = {}
	    for sLine in fHandle.readLine do
		  tLines[#tLines+1] = sLine
	    end
	    fHandle.close()
	    return tLines
  end
end
local function writeLines(sPath, tLines)
  local fHandle = fs.open(sPath, 'w')
  if fHandle then
	    fHandle.writeLine(table.concat(tLines, '\n'))
	    fHandle.close()
  end
end
help = {}
help[1] = "yo yo yo"
help[2] = "everybody"
writeLines("newFile", help)

and it created a file named "newFile" and inserted

yo yo yo
everybody
IsaacTBeast #5
Posted 29 May 2014 - 08:29 AM
Where should I put the code to my program?
IsaacTBeast #6
Posted 29 May 2014 - 08:58 AM
Thank you anyways :(/>
Blue #7
Posted 29 May 2014 - 09:20 AM
To prevent having to write the code do this:(so it doesn't waste your time and prevents errors)
1.create a file in computercraft using edit (optional)
2.If you're not on a server then go to your (real-life) computer then run "%Appdata%
3.Go to .minecraft/saves/<save>/computer/<ID>
4.You should now see your computercraft files,open the one you want with a text editor.
5.Copy and paste the code and save
You can now do whatever you like with the file.(If you are on a server then talk to an admin)


Hope that this helps :)/>

EDIT: Also,say the line where it errors or doesn't work
Edited on 29 May 2014 - 07:25 AM
IsaacTBeast #8
Posted 29 May 2014 - 04:17 PM
I know it Glass :)/> but I know it before I joined CC. :)/>