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

how to make a program that shows file's content?

Started by IsaacTBeast, 28 May 2014 - 01:48 PM
IsaacTBeast #1
Posted 28 May 2014 - 03:48 PM
This is my logic.

file.txt content:
This is a test file.


Command:
output -o file.txt

then it shows the content of the file.. like this:

Title of the File: file.txt
Content:

This is a test file.
CometWolf #2
Posted 28 May 2014 - 03:49 PM
Open the file in read mode with the fs API, then just print(file.readAll())
Edited on 28 May 2014 - 01:50 PM
IsaacTBeast #3
Posted 28 May 2014 - 04:00 PM
Where should I put the file.readAll() ?


function printUsage()
   print("Usage: file [(OPTION)] [(File Name)]")
   print(" ")
   print("-find	-- Is an option that finds a file.")
   print(" ")
   print("-o	 -- It shows the output of your file content.")
end
-- This program was created by IsaacTBest.
-- This is OpenSource
-- 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
			 printUsage();
	   end
   end
else
	printError( "File does not exist!" )
end
end
if sCommand == "-o" then
  local sPath = shell.resolve( tArgs[2] )
  local tFiles = fs.find( sPath )
  for n,sFile in ipairs( tFiles ) do
	if fs.exists( sFile ) then
	  print("Title of file: "..sFile)
	  print("Content:")
	  print(file.readAll())
	else
	  printError( "File does not exist!" )
	end
  end
end

Edited on 28 May 2014 - 02:11 PM
CometWolf #4
Posted 28 May 2014 - 04:16 PM
Right where you did, but you have to open the file first obviously. I assume you've used fs.open before?
IsaacTBeast #5
Posted 28 May 2014 - 04:19 PM
Right where you did, but you have to open the file first obviously. I assume you've used fs.open before?


function printUsage()
   print("Usage: file [(OPTION)] [(File Name)]")
   print(" ")
   print("-find	-- Is an option that finds a file.")
   print(" ")
   print("-o	 -- It shows the output of your file content.")
end
-- This program was created by IsaacTBest.
-- This program is OpenSource.
-- 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
			 printUsage();
	   end
   end
else
	printError( "File does not exist!" )
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 ) // I open the file here. I think it doens't work?
		 print("Title of file: "..sFile)
		 print("Content:")
		 print(file.readAll( sFile ))
		 file.close()
	   else
		 printError( "File does not exist!" )
	   end
	end
  end
end

Edited on 28 May 2014 - 02:19 PM
CometWolf #6
Posted 28 May 2014 - 04:35 PM
Just answer next time. You clearly haven't used it.
http://computercraft.info/wiki/Fs.open
the mode you need is read ("r")

local file = fs.open(fileName,mode)
print(file.readAll())
file.close()
IsaacTBeast #7
Posted 28 May 2014 - 04:36 PM
Thank you :D/> it works :D/>!!