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

Help

Started by Leon669, 20 February 2015 - 07:17 PM
Leon669 #1
Posted 20 February 2015 - 08:17 PM
How can i delete the hole text of a file with fs.open?
InDieTasten #2
Posted 20 February 2015 - 08:20 PM

local fh = fs.open("pathtofiletodelete", "w")
fh.close()
? Not sure, but it'll do what I interpret of your question
Edited on 20 February 2015 - 07:23 PM
Quintuple Agent #3
Posted 20 February 2015 - 08:21 PM
Well, first of all, you can use fs.delete to delete a file, but if you must use fs.open, doing fs.open(file,"w") will open it in write mode, which means the file is cleared and whatever is written to it using fs.write() or fs.writeLine() will be the only data in the file, so if you were to do

a=fs.open(file,"w")
a.close()
That would effectively delete the contents of the file, however the file will still be there, just nothing in it.
Leon669 #4
Posted 20 February 2015 - 08:28 PM
Well, first of all, you can use fs.delete to delete a file, but if you must use fs.open, doing fs.open(file,"w") will open it in write mode, which means the file is cleared and whatever is written to it using fs.write() or fs.writeLine() will be the only data in the file, so if you were to do

a=fs.open(file,"w")
a.close()
That would effectively delete the contents of the file, however the file will still be there, just nothing in it.

Thanks that was that iam searching for.
Leon669 #5
Posted 20 February 2015 - 09:24 PM
shell.run"cd //"
file = fs.open("loginSettings/curUser", "r")
version = file.readLine()
file.close()
term.setCursorPos(1,9)
print(version)
shell.run"cd //"
shell.run"cd OS"

get an error by reading a file
attempt to index? a nil value
InDieTasten #6
Posted 20 February 2015 - 09:34 PM
I would imagine your file "loginSettings/curUser" doesn't exist, therefor your variable file will be nil, and next line you are attempting to index this nil variable via .readLine Which leads to error at that point. Make sure the file is existing or/and check the variable to not be nil, before indexing it.

Also note, that you are giving a relative path to fs.open meaning running your script from another location will lead in another resolved path. So you might want to change it to an absolute path by anchoring it to the root of the filesystem: "/loginSettings/curUser"
Edited on 20 February 2015 - 08:36 PM