io.open (filename, mode) #
화일을 열고 그 화일에 해당하는 화일 핸들을 반환한다. 에러일 경우에는 nil을 반환한다.
mode는 string 형으로 지정하며 옵션은 다음과 같다.
b문자를 mode 문자열의 끝에 첨가하면 이진화일형태로 열게 된다.
| "r" | 읽기모드 |
| "w" | 쓰기모드 |
| "a" | 첨부모드 |
| "r+" | 갱신모드. 읽기모드지만 이전 화일은 삭제. |
| "w+" | 갱신모드. 쓰기모드지만 이전 화일은 삭제. |
| "a+" | 첨부모드지만 화일의 끝에서부터만 쓰기가 허용. |
b문자를 mode 문자열의 끝에 첨가하면 이진화일형태로 열게 된다.
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.









![[http]](/wiki/imgs/http.png)
