66 posts
Posted 02 September 2012 - 06:44 AM
hello, i want to know how i would edit a file by doing fs.open then how would i write something, how would i read from it, and how would i be able to erase 1 of the many things on the list? because i want to make a door lock that has a number gen and puts a random number onto a disk, and when you inserted the disk that had a number on it, it would read the .txt file and see if any of the random numbers that are on the list match, and anybody who had a admin card could erase numbers without having to do edit pass.txt, there would be a option on a menu that would take him to a screen that had a list of all the numbers and he could enter one of the many numbers and it would erase it from the list.
864 posts
Location
Sometime.
Posted 02 September 2012 - 08:30 AM
Its just like io.open
hFile = fs.open("file", "r")
repeat
lines[i] = hFile.readLine()
i = i + 1
until lines[i] = "" or lines[i] = nil -- not sure if its '=' or '=='
hFile.close()
for k, v in pairs(lines)
if lines[v] == "--bler" then
lines[v] = ""
end
end
13 posts
Posted 02 September 2012 - 08:49 AM
here is a program that reads a file, you open it in "r" or reading mode, this appends the text, if you open it without i think it erases all the text in the file
write("Enter Path/FileName:")
filename = io.read()
if fs.exists(filename) == true then
h = fs.open(filename, "r")
text = h.readAll()
h.close()
print(text)
else
print("file does not exist, check path..")
end
this program will create a file called example.txt in "w" or write mode, this erases all the text when you open it… im pretty sure.
h = fs.open("example.txt", "w")
io.write("Data>>")
text = io.read()
h.write(text)
h.close()
you can also do h.writeLine("blah blah"), this would place blah blah in the file followed by a return.
66 posts
Posted 02 September 2012 - 10:10 AM
Its just like io.open
hFile = fs.open("file", "r")
repeat
lines[i] = hFile.readLine()
i = i + 1
until lines[i] = "" or lines[i] = nil -- not sure if its '=' or '=='
hFile.close()
for k, v in pairs(lines)
if lines[v] == "--bler" then
lines[v] = ""
end
end
here is a program that reads a file, you open it in "r" or reading mode, this appends the text, if you open it without i think it erases all the text in the file
write("Enter Path/FileName:")
filename = io.read()
if fs.exists(filename) == true then
h = fs.open(filename, "r")
text = h.readAll()
h.close()
print(text)
else
print("file does not exist, check path..")
end
this program will create a file called example.txt in "w" or write mode, this erases all the text when you open it… im pretty sure.
h = fs.open("example.txt", "w")
io.write("Data>>")
text = io.read()
h.write(text)
h.close()
you can also do h.writeLine("blah blah"), this would place blah blah in the file followed by a return.
lets say i have it in a list like so
25789 24593 75469 84452
and i wanted to remove 24593, how would i do that with code?
13 posts
Posted 03 September 2012 - 04:44 PM
Its just like io.open
hFile = fs.open("file", "r")
repeat
lines[i] = hFile.readLine()
i = i + 1
until lines[i] = "" or lines[i] = nil -- not sure if its '=' or '=='
hFile.close()
for k, v in pairs(lines)
if lines[v] == "--bler" then
lines[v] = ""
end
end
here is a program that reads a file, you open it in "r" or reading mode, this appends the text, if you open it without i think it erases all the text in the file
write("Enter Path/FileName:")
filename = io.read()
if fs.exists(filename) == true then
h = fs.open(filename, "r")
text = h.readAll()
h.close()
print(text)
else
print("file does not exist, check path..")
end
this program will create a file called example.txt in "w" or write mode, this erases all the text when you open it… im pretty sure.
h = fs.open("example.txt", "w")
io.write("Data>>")
text = io.read()
h.write(text)
h.close()
you can also do h.writeLine("blah blah"), this would place blah blah in the file followed by a return.
lets say i have it in a list like so
25789 24593 75469 84452
and i wanted to remove 24593, how would i do that with code?
yeah i couldn't figure that out haha, but you could try making them all in separate files (empty with number as name or some other system) and then just remove that file and use fs.exists() to keep track??
5 posts
Posted 03 September 2012 - 07:21 PM
http://rosettacode.org/wiki/Remove_lines_from_a_fileawesome site, with the remove lines code. Just tweak it a bit and its ready to go.
Oh and that 'r' is read, 'w' clears and writes new, and 'w+' appends at the END of the file (as far as I know).
13 posts
Posted 04 September 2012 - 04:26 AM
http://rosettacode.o...nes_from_a_fileawesome site, with the remove lines code. Just tweak it a bit and its ready to go.
Oh and that 'r' is read, 'w' clears and writes new, and 'w+' appends at the END of the file (as far as I know).
"w+", that will come in handy cheers man.
1688 posts
Location
'MURICA
Posted 04 September 2012 - 04:36 AM
I think the best way to go about doing this is to use the gsub() method on the content of the file.
local filename = 'your/path'
local file = fs.open(filename,'r')
local data = file.readAll()
file.close()
data = data:gsub('whatever text you want to remove', '')
file = fs.open(filename,'w')
file.write(data)
file.close()