2012-03-17 56 views
1

所以我試圖做出我的第一個lua程序,我無法弄清楚爲什麼當我在一個文件中讀取它返回的文件不存在或不在目錄中。 ..這是我的代碼。Lua在一個文件中讀取

io.input("myfile.txt") 
word = io.read("*line") 
print (word) 

myfile.txt與項目位於同一目錄中....可能是什麼錯誤?

+1

「myfile.txt的是在相同的目錄項目」是什麼項目?據我所知,Lua沒有項目。 – 2012-03-17 22:47:30

+1

如果您沒有限定路徑,文件需要與執行lua腳本的解釋程序在同一目錄中。 – 2012-03-17 23:52:49

+0

你好嗎? myfile.txt需要位於運行腳本的*工作目錄*中;取決於你如何調用它,這可能與你的腳本文件所在的目錄不同! – ToxicFrog 2016-07-10 17:09:07

回答

1

我不能重複這個問題:

: [email protected] 10317 ; echo foo > myfile.txt 
: [email protected] 10318 ; lua 
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio 
> io.input("myfile.txt") 
> word = io.read("*line") 
> print (word) 
foo 
>