E D R S I H C RSS
ID
Password
Join
환상이 없는 곳에 과학이 없고, 사실을 무시하면 예술이 성립되지 않는다. ―블라디미르 나보코프

 * 별거 아니지만 까먹을 것같아 옮겨놓습니다. 루아의 io.open()함수가 C의 fopen()과 거의 같더군요. text만 되는 줄 알았더니 바이너리도 잘된다는...^^ 덕분에 ACE와 합치면 손쉽게 화일 전송기능을 구현할 수 있을 듯 합니다.
  • 다음 예제는 test.bmp라는 화일을 읽어서 test2.bmp 화일로 128바이트씩 나누어 저장하는 코드입니다.
fh = io.open("test.bmp", "rb")
os.remove("test2.bmp")
while true do
    local t = fh:read(128)
    print(t)
    if t == nil then
    	break
    end
    local fh2 = io.open("test2.bmp", "ab")	
    fh2:seek("end")
    fh2:write(t)
    fh2:close()
end
fh:close()
  • 다음은 피클링을 살짝 적용해본 예제. LUA : Pickle 참조.
require("pickle")
fh = io.open("test.bmp", "rb")
os.remove("test2.bmp")
tmp = {}
while true do
    tmp.size = 128
    tmp.buf = fh:read(128)
    if tmp.buf == nil then
    	break
    end
    local s = pickle(tmp)
    print(s)
    print("피클된 문자열 길이 :", string.len(s))

    --구분을 위한 공백

    local t2 = unpickle(s)
    local fh2 = io.open("test2.bmp", "ab")	
    fh2:seek("end")
    fh2:write(t2.buf)
    fh2:close()
end
fh:close()

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2010-10-28 12:42:52
Processing time 0.0889 sec