简体   繁体   English

读取文件之前Lua无法打印

[英]Lua not printing before file read

I'm having an issue getting Lua to print before running a file open and read. 我在运行Lua打开和读取文件之前无法打印Lua时遇到问题。 This will print the strings "Reading File..." and "File Read!" 这将打印字符串“ Reading File ...”和“ File Read!”。 but only after it has completed the getFileString() function. 但只有在完成getFileString()函数之后。 I want it to print "Reading File..." before it runs. 我希望它在运行前打印“ Reading File ...”。 I narrowed it down to file:read("*a") which is messing up all the prints in my (larger) script. 我将其范围缩小到file:read(“ * a”),这弄乱了我(较大)脚本中的所有打印件。

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. 我应该提到它主要是因为我正在读取150MB左右的文件。

I think the output is simply buffered. 我认为输出只是缓冲。 Try adding io.stdout:setvbuf('no') before printing, which should turn buffering of output off. 尝试在打印之前添加io.stdout:setvbuf('no') ,这将关闭输出缓冲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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