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

Computers writing to text files?

Started by Angry_Dragonoid, 21 February 2016 - 03:18 AM
Angry_Dragonoid #1
Posted 21 February 2016 - 04:18 AM
Hello,
I am trying to make a sort of Computer inventory system. I can probably figure out the main thing by myself, but I think that it would be kinda neat to make the computers to post the inventory info, from a table that it writes, to a text file, (still in the computer) and print it, (still in game) to make a sort of Inventory Report.

I was really just wondering if it's possible or not. If it is, then please tell me how, I don't have any code written up just yet, so please don't ask.


Angry_Dragonoid
HPWebcamAble #2
Posted 21 February 2016 - 05:49 AM
So basically, it'll need to:
  • Get input from the user to add new items
  • Display the current info
  • Save and load from a file when appropriate
  • Possibly delete or rearrange items
Totally possible, but depending on how complex you were thinking, it might take a bit to create.
Let us know if you aren't sure where to start, or you get stuck on something :)/>
Dog #3
Posted 21 February 2016 - 06:12 AM
Here are a few wiki links that should help get you started…

For reading from and writing to the file, you'll want to read up on fs.open. Don't forget to close your file handle (h.close) after you are done reading/writing or you'll run into problems.

In order to write a table properly, you'll need to use textutils.serialize before writing to the file. Use textutils.unserialize when reading the table back from the file.

If you're still stuck, let us know :)/>
Angry_Dragonoid #4
Posted 25 February 2016 - 02:32 AM
Erm…not sure if you exactly understand what I want. I believe you are referring to creating a using a text file on my physical laptop computer, like a notepad file. I mean, the text writing program in game, in the computer/turtle, then it'll save it, be able to edit it, and be able to print it STILL IN GAME, using the in-game printer, on command. Now, using math programs, or management programs, it's my specialty. But anything with tables…phhh…no clue…pls help!! XD
KingofGamesYami #5
Posted 25 February 2016 - 02:47 AM
Dog is describing how to use files in game.

<offtopic> (sort of):
In game files are still stored on your real computer, under ~.minecraft/saves/[world]/computer/[computer id]/[name of file].
</offtopic>

A simple example of file manipulation:


local tArgs = {...} --#get the program arguments
local file = fs.open( tArgs[ 1 ], #tArgs > 1 and "w" or "r" ) --#if we have more than one argument, open in write mode.  Otherwise, open in read mode.
if #tArgs > 1 then --#if we have more than 1 argument, 
  file.write( table.concat( tArgs, " ", 2 ) ) --#write the rest of the arguments (separated by a space) to the file
else --#otherwise,
  print( file.readAll() ) --#print the contents of the file
end
file.close() --#always close your file handles!

Usage:
program test hello world –> writes "hello world" to the file "test"
program test –> prints the contents of test
Edited on 25 February 2016 - 01:48 AM
Silver07 #6
Posted 04 March 2016 - 06:16 AM
use the fs api
fileHandle=fs.open("filename","w")
w write
wb write bytes

r read
rb read bytes
a append (write without resetting file, useful for logging)
ab append bytes
in the shell use "help fsapi" for more info