4
votes

I'm having an issue getting Lua to print before running a file open and read. This will print the strings "Reading File..." and "File Read!" but only after it has completed the getFileString() function. I want it to print "Reading File..." before it runs. I narrowed it down to file:read("*a") which is messing up all the prints in my (larger) script.

function getFileString(path)

    local file, err = io.open(path, "r")
    local all = file:read("*a")
    file:close()

    return all

end

function main()

    local directory = "C:\\Documents and Settings\\All Users\\Documents\\"

    print("Reading File...")
    local file_all = getFileString(directory.."myFile.txt")
    print("File Read!\n")

end

main()

It also didn't seem to matter whether I functionalised or not. I should mention that it's noticeable mainly as I am reading a 150MB or so file.

1
What development environment are you using. On the one I use the output window is only refreshed on end of script or when a breakpoint is reached. - Jane T
Yes, this is certainly an issue due to the IDE (SciTE?). Lua itself for sure executes the first print before reading the file. - prapin
yup, this is probably a buffering issue with your editor. if you run lua.exe and call dofile("name_of_script.lua") - i think you'll get the results you expect. - Mike Corcoran

1 Answers

6
votes

I think the output is simply buffered. Try adding io.stdout:setvbuf('no') before printing, which should turn buffering of output off.