简体   繁体   中英

Lua not printing before file read

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM