U E D R S I H C RSS
ID
Password
Join
사탄이 한숨 지으며 하는 말, "기독교인들은 지옥을 나보다 더 잘 알고 있단 말이야." - A. 클레임보그

개요 #

  • 이 메뉴얼을 5.0 알파버젼을 기준으로 작성된 것입니다.
  • 루아에서의 화일 제어는 핸들 획득 -> 핸들 사용의 두가지로 분류할 수 있습니다.

핸들 획득 함수들 #

io.open (filename, mode) #

화일을 열고 그 화일에 해당하는 화일 핸들을 반환한다. 에러일 경우에는 nil을 반환한다. mode는 string 형으로 지정하며 옵션은 다음과 같다.
"r"읽기모드
"w"쓰기모드
"a"첨부모드
"r+"갱신모드. 읽기모드지만 이전 화일은 삭제.
"w+"갱신모드. 쓰기모드지만 이전 화일은 삭제.
"a+"첨부모드지만 화일의 끝에서부터만 쓰기가 허용.

b문자를 mode 문자열의 끝에 첨가하면 이진화일형태로 열게 된다.

io.tmpfile() #

임시화일을 생성하고 그 화일에 대한 핸들을 반환한다. 임시화일은 차후 프로그램이 종료하면 자동으로 삭제된다.

핸들 사용 함수들 #

fh는 위의 핸들 획득 함수들의 반환값인 화일 핸들을 의미한다.

fh:close() #

해당 화일을 닫는다.

fh:flush() #

현재 버퍼에 담겨있는 기록중인 데이타를 모두 화일에 적용한다.

fh:read(포맷문자열, ...) #

화일 핸들 fh로부터 주어진 포맷에 따라 데이타를 읽어들여 그 값을 반환한다. 포맷문자열안에 들어가는 명령은 다음과 같다.
"*n"숫자를 읽어들여 반환한다. 이것은 number 타입으로 반환하는 유일한 포맷이다.
"*a"현재 위치에서 나머지 전체 내용을 읽어들여 그 내용을 반환. 화일의 끝에서 명령을 실행한거라면 nil반환.
"*l"다음 줄을 읽어들인다.(EOF는 건너뛴다) 화일의 끝에서 명령을 실행한거라면 nil반환. 이것이 기본 포맷임.
숫자주어진 숫자만큼의 문자를 읽어들여 그 내용을 반환한다. 0을 지정하면 아무것도 읽지않고 텅빈 문자열을 반환한다. 화일의 끝에서 명령을 실행한거라면 nil반환.

예제:
myfile = open("test.txt","r")
t1 = myfile:read(3) -- t1에 3글자만큼 문자열을 읽어서 기록(문자열)
n2 = myfile:read("*n") -- n1에 숫자를 읽어 그 값을 기록(숫자)
t3 = myfile:read() -- 한줄을 모두 읽어 t3에 기록(문자열)

fh:seek (whence [, offset]) #

Sets and gets the file position, measured in bytes from the beginning of the file, to the position given by offset plus a base specified by the string whence, as follows:
“set” base is position 0 (beginning of the file);
“cur” base is current position;
“end” base is end of file;

In case of success, function seek returns the final file position, measured in bytes from the beginning of the file. If this function fails, it returns nil, plus a string describing the error. The default value for whence is "cur", and for offset is 0. Therefore, the call file:seek() returns the current file position, without changing it; the call file:seek("set") sets the position to the beginning of the file (and returns 0); and the call file:seek("end") sets the position to the end of the file, and returns its size.

fh:write (value1, ...) #

Writes the value of each of its arguments to the filehandle fh. The arguments must be strings or numbers. To write other values, use tostring or format before write. If this function fails, it returns nil, plus a string describing the error.



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