|
Post by podahmad on Jul 24, 2019 5:40:35 GMT
i want to read lines in a file then compare it to a string in order to find if the string is present in the file if it is present then fine..if not then append it to the file
eg file contents are a b c
i read the file line by line using for line in fh:lines() do i find that d is not there and append it
how can i do it
|
|
|
Post by AnkuLua on Jul 24, 2019 8:29:30 GMT
i want to read lines in a file then compare it to a string in order to find if the string is present in the file if it is present then fine..if not then append it to the file eg file contents are a b c i read the file line by line using for line in fh:lines() do i find that d is not there and append it how can i do it Use "a+" mode when opening a file. fPointer = io.open(fileName, "a+") www.tutorialspoint.com/lua/lua_file_io
|
|
|
Post by podahmad on Jul 24, 2019 11:56:04 GMT
i want to read lines in a file then compare it to a string in order to find if the string is present in the file if it is present then fine..if not then append it to the file eg file contents are a b c i read the file line by line using for line in fh:lines() do i find that d is not there and append it how can i do it Use "a+" mode when opening a file. fPointer = io.open(fileName, "a+") www.tutorialspoint.com/lua/lua_file_ioive done that.. but the problem is how do i compare each line with a string if statement with == is not working here
|
|
|
Post by AnkuLua on Jul 24, 2019 12:22:51 GMT
Please make sure that the file is save in "LF" end-of-line format, not "CR-LR". If still with problem, please share the script and files.
PS: You need to use fPointer:seek("set") to go to the beginning of the file if opening the file with "a+".
|
|
|
Post by podahmad on Jul 25, 2019 16:43:16 GMT
Please make sure that the file is save in "LF" end-of-line format, not "CR-LR". If still with problem, please share the script and files. PS: You need to use fPointer:seek("set") to go to the beginning of the file if opening the file with "a+". ill try this.. thoug i was able to do it by mounting the whole file to a table first then do the required comparison content = {} for line in fh:lines() do table.insert (content, line) end
|
|
|
Post by podahmad on Jul 27, 2019 4:58:50 GMT
Please make sure that the file is save in "LF" end-of-line format, not "CR-LR". If still with problem, please share the script and files. PS: You need to use fPointer:seek("set") to go to the beginning of the file if opening the file with "a+". great..it workd is there anyway to remove line from file
|
|
|
Post by podahmad on Jul 27, 2019 5:24:03 GMT
Please make sure that the file is save in "LF" end-of-line format, not "CR-LR". If still with problem, please share the script and files. PS: You need to use fPointer:seek("set") to go to the beginning of the file if opening the file with "a+". im stuck with what i want to do..so help plz ill write the scenario in simple way ive the txt file by the name of logs which contents are a e c b d and i want to remove the letter b from the log file file=scriptPath() .. "logs.txt" a=0 fh,err = io.open(file, "r") for line in fh:lines() do if line=='b' then a=1 --here we found that the file has b in it break end end io.close(fh) if a==1 then (what should i write here to remove the line that contains the letter b in it) end
|
|
|
Post by AnkuLua on Jul 27, 2019 7:25:35 GMT
Try the following to remove lines equal to b.
file=scriptPath() .. "logs.txt" fh,err = io.open(file, "r")
outFile = scriptPath() .. "out.txt" outFh = io.open(outFile, "w") for line in fh:lines() do
if line ~='b' then outFh:write(line .. "\n") end
end
io.close(fh) io.close(outFh)
|
|
|
Post by podahmad on Jul 27, 2019 8:46:53 GMT
Try the following to remove lines equal to b. file=scriptPath() .. "logs.txt" fh,err = io.open(file, "r")
outFile = scriptPath() .. "out.txt" outFh = io.open(outFile, "w") for line in fh:lines() do
if line ~='b' then outFh:write(line .. "\n") end
end
io.close(fh) io.close(outFh)
so there is no way of accessing a file lines by indices maybe this does not make a beautiful code and is a little risky moving it to a temporary file nonetheless it works
|
|
|
Post by podahmad on Jul 27, 2019 12:19:53 GMT
Try the following to remove lines equal to b. file=scriptPath() .. "logs.txt" fh,err = io.open(file, "r")
outFile = scriptPath() .. "out.txt" outFh = io.open(outFile, "w") for line in fh:lines() do
if line ~='b' then outFh:write(line .. "\n") end
end
io.close(fh) io.close(outFh)
im accessing the file from 4 emulator..this is not how i intend it to work is there any other way
|
|
|
Post by AnkuLua on Jul 28, 2019 0:03:16 GMT
Did you want to access the same file from 4 emulators at the same time? You can put it on cloud drive and read it.
But you can't modify the cloud file.
|
|