Jump to content




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


  • You cannot reply to this topic
6 replies to this topic

#1 IsaacTBeast

  • Members
  • 86 posts
  • LocationI 'm inside your brian

Posted 28 May 2014 - 02: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.

#2 CometWolf

  • Members
  • 1,283 posts

Posted 28 May 2014 - 02:49 PM

Open the file in read mode with the fs API, then just print(file.readAll())

Edited by CometWolf, 28 May 2014 - 02:50 PM.


#3 IsaacTBeast

  • Members
  • 86 posts
  • LocationI 'm inside your brian

Posted 28 May 2014 - 03: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 by IsaacTBeast, 28 May 2014 - 03:11 PM.


#4 CometWolf

  • Members
  • 1,283 posts

Posted 28 May 2014 - 03:16 PM

Right where you did, but you have to open the file first obviously. I assume you've used fs.open before?

#5 IsaacTBeast

  • Members
  • 86 posts
  • LocationI 'm inside your brian

Posted 28 May 2014 - 03:19 PM

View PostCometWolf, on 28 May 2014 - 03:16 PM, said:

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 by IsaacTBeast, 28 May 2014 - 03:19 PM.


#6 CometWolf

  • Members
  • 1,283 posts

Posted 28 May 2014 - 03: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()


#7 IsaacTBeast

  • Members
  • 86 posts
  • LocationI 'm inside your brian

Posted 28 May 2014 - 03:36 PM

Thank you :D it works :D!!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users