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

readAll() not working

Started by Waitdev_, 05 June 2015 - 10:20 AM
Waitdev_ #1
Posted 05 June 2015 - 12:20 PM
i was messing around with files and images, then i hit a problem :/
i was making a file to copy code from one file and paste it onto another file after what the file already has, and then readAll() didn't work (said it didn't exist.)
i'm sure i'm doing it right, so heres the code.

tArgs = {...}
print("Compressing 2 files:"..tArgs[1].." and "..tArgs[2])
local from = fs.open(tostring(tArgs[1]),"r")
local to = fs.open(tostring(tArgs[2]),"w")
local data = from.readAll() --where theres the problem.
to.write(data)
from.close()
to.close()
Edited on 05 June 2015 - 10:47 AM
DannySMc #2
Posted 05 June 2015 - 12:38 PM
You need to choose the mode in which to open by, write, read, append…

Write mode allows you to write into a file and read mode allows you to read a file using .readAll()

Use fs.open("filename", "w") to open and write a file and use fs.open("filename", "r") to read a file!

:)/>/>

So from needs to be "r" and to needs to be "w"
Edited on 05 June 2015 - 10:39 AM
Waitdev_ #3
Posted 05 June 2015 - 12:42 PM
oops… forgot to put that there in the forum code, but i have them both on computercraft :/
Edited on 05 June 2015 - 10:43 AM
DannySMc #4
Posted 05 June 2015 - 02:02 PM
Well I have just tried the code and it works perfectly… No errors, I would make sure that you are using the correct file paths when you do it, so it would be <programname> <from> <to>

If you are using an emulator that might be why, I use cclite and it has never failed me so :P/>

Would make sure you are supplying the correct paths
Edited on 05 June 2015 - 12:03 PM
Lyqyd #5
Posted 05 June 2015 - 02:34 PM
Since you didn't copy and paste the code directly from what you're using in Minecraft, please go through it character by character and verify that they are exactly the same. It would be best if you could just upload the code that doesn't work directly to pastebin.
Waitdev_ #6
Posted 05 June 2015 - 02:56 PM
its saying
attempt to index ? (a nil value)
flaghacker #7
Posted 05 June 2015 - 03:09 PM
its saying
attempt to index ? (a nil value)

Upload your program's code to pastebin, using

pastebin put <fileName>
and come here with the paste id it returns.
MKlegoman357 #8
Posted 05 June 2015 - 03:58 PM
Are you sure that those files exist? You should probably be checking for that, as well as if you're not trying to edit a directory. Also, you probably know this but just to be safe: there's an fs.copy( from, to ) function made specificaly for that.
Square789 #9
Posted 05 June 2015 - 07:57 PM
fs.open(file,mode)
does not work like
fs.open(CurrentDir..file,mode)
I guess, you're running the file inside a directory, so if you are in the directory bar:
bar> FILENAME foo foo2
will check for the files

foo and foo2

but not for the files

/bar/foo and /bar/foo2

So make

fs.open(tostring(tArgs[1]),"r")
to

crrdir = shell.dir()
fs.open(crrdir.."/"..tostring(tArgs[1]),"r")
and you should be fine.
Edited on 05 June 2015 - 05:59 PM
Waitdev_ #10
Posted 06 June 2015 - 01:17 AM
Are you sure that those files exist? You should probably be checking for that, as well as if you're not trying to edit a directory. Also, you probably know this but just to be safe: there's an fs.copy( from, to ) function made specificaly for that.
i'll try that, thanks
fs.open(file,mode)
does not work like
fs.open(CurrentDir..file,mode)
I guess, you're running the file inside a directory, so if you are in the directory bar:
bar> FILENAME foo foo2
will check for the files

foo and foo2

but not for the files

/bar/foo and /bar/foo2

So make

fs.open(tostring(tArgs[1]),"r")
to

crrdir = shell.dir()
fs.open(crrdir.."/"..tostring(tArgs[1]),"r")
and you should be fine.
i'll try this aswell, should be working well after these ;)/> thanks
Bomb Bloke #11
Posted 06 June 2015 - 04:09 AM
So make

fs.open(tostring(tArgs[1]),"r")
to

crrdir = shell.dir()
fs.open(crrdir.."/"..tostring(tArgs[1]),"r")
and you should be fine.

A slightly easier method is to use shell.resolve():

fs.open(shell.resolve(tArgs[1]),"r")

Note that you need not tostring() program arguments coming in from the command prompt - they're always strings unless you go out of your way to change them.

I get the impression you want to combine your two files. Remember that write mode ("w") deletes the contents of the original file! If you wish to append new data to the end of an existing file, while keeping the original contents, use append mode ("a").
Waitdev_ #12
Posted 06 June 2015 - 04:18 AM
thanks, all of this worked ;)/>