2012-05-23 3 views
10

Lua를 사용하여 파일 크기를 가져 오는 데 문제가 있습니다. 파일의 파일 크기가 743 bytes이면 파일이 삭제되는 함수 메소드를 작성 중입니다.루아에서 파일 크기와 파일을 삭제하는 방법은 무엇입니까?

local getDLFile = function(fileToDL) 
      local path = system.pathForFile(fileToDL, system.DocumentsDirectory) 
      local myFile = io.open(path, "w+b") 
      http.request{ 
       url = "http://www.testfile.com/"..fileToDL, 
       sink = ltn12.sink.file(myFile), 
      } 

      -- i don't know what is the syntax 
      if myFile.size == 743 bytes then 
       myFile.delete 
      end    

end 

누구도 날이 내 사건에 대해 도움을 줄 수 있습니다 : 여기

내 코드?

답변

18

크기는 myFile:seek("end")입니다.

파일을 삭제하려면 os.remove(path)을 사용하십시오. 하지만 먼저 파일을 닫으십시오.

+0

감사 LHF을 삭제하려면 여기 http://keplerproject.github.com/luafilesystem/manual.html#reference

를 읽을 수 있습니다

local lfs = require "lfs" local size = lfs.attributes (path, "size") 

을 사용하여 파일 크기를 얻을 수있다, 난 (인쇄'myfile에 시도 : seek ("end"))'하지만 내 코로나에서 이런 오류가 발생했다. 런타임 오류 ... ne \ 내 문서 \ 노래 우상 최종 게임 \ freeplay.lua : 258 : 시도 시도 닫힌 파일 사용 스택 추적 : [C] :? [C] : 함수 'seek' ... ne \ 내 문서 \ 노래 우상 최종 게임 \ freeplay.lua : 258 : 기능에서 'getDLFile' ... ne \ my d' – gadss

+1

@ 가제트, 그것 그 파일을 닫을 때 찾기 전에 파일을 닫은 것 같습니다. – lhf

+0

여기를 참조하십시오. http://www.lua.org/pil/21.3.html – frgtv10

7

최근 Lua 파일 시스템 지원이 코로나에 추가되었습니다! 당신은 당신은 당신의 대답에 대한 파일 사용을

os.remove(path) 
관련 문제