티스토리 뷰

728x90

위의 그림은 고도 스크립트에서 파일 클래스를 사용하는 대표적인 사례로 클래스 인스턴스를 만들고, 파일을 열고, 작업 후 닫는 전형적인 과정을 거친다. 사용할 수 있는 다양한 메서드는 다음과 같다. 파일 읽기 및 쓰기는 UTF-8 인코딩으로 처리한다.

 

■ 열기, 닫기

Error open ( String path, ModeFlags flags )

   ☞ 정상시 OK 리턴, READ/WRITE/READ_WRITE/WRITE_READ(파일 존재하면 삭제함) 모드 사용 가능 
Error open_compressed ( String path, ModeFlags mode_flags, CompressionMode compression_mode=0 )

   ☞ 압축된 파일 열기, 단 고도에서 저장한 파일만 읽기 가능. 압축 방식은 COMPRESSION_FASTLZ, COMPRESSION_DEFLATE, COMPRESSION_ZSTD, COMPRESSION_GZIP 사용 가능
Error open_encrypted ( String path, ModeFlags mode_flags, PoolByteArray key )

   ☞ 32비트 이진 키로 암호화된 파일 열기
Error open_encrypted_with_pass ( String path, ModeFlags mode_flags, String pass )

   ☞ 비밀번호로 암호화된 파일 열기
void close ( )

   ☞ 닫기

 

■ 읽기, 쓰기

int get_8 ( ) const, int get_16 ( ) const, int get_32 ( ) const, int get_64 ( ) const

   ☞ 8~64 비트를 읽어 정수로 처리
float get_float ( ) const, float get_double ( ) const, float get_real ( ) const

   ☞ 32, 64 비트를 읽어 실수로 처리. get_real은 파일 내용에 따라 32/64비트 적용 여부 판단
String get_as_text ( ) const

   ☞ 파일 전체를 스트링으로 읽기.
PoolByteArray get_buffer ( int len ) const

   ☞ 다음 len 바이트 읽기
PoolStringArray get_csv_line ( String delim="," ) const

   ☞ 다음 라인을 CSV 파일 형식으로 읽어 문자열 배열 리턴. 기본은 콤마(,)로 항목을 구분하지만 변경할 수 있다.
String get_line ( ) const

   ☞ 다음 라인을 읽어 스트링으로 리턴
String get_pascal_string ( )

   ☞ 파스칼 형식의 문자열(문자열 길이도 저장) 읽기
Variant get_var ( bool allow_objects=false ) const

   ☞ 동적 변수 읽기. 오브젝트 허용을 true로 설정하면 오브젝트도 읽을 수 있다.

 

void store_8 ( int value ), void store_16 ( int value ), void store_32 ( int value ), void store_64 ( int value )

   ☞ 정수값을 8~64 비트로 쓰기. 값이 지정 범위를 넘어서면 잘림. 부호가 있는 정수는 store_64 사용.
void store_float ( float value ), void store_double ( float value ), void store_real ( float value )

   ☞ 실수값을 32, 64 비트로 쓰기. store_real은 변수에 따라 32/64 비트로 쓰기
void store_string ( String string )

   ☞ 문자열 쓰기
void store_buffer ( PoolByteArray buffer )

   ☞ 바이트 배열 쓰기
void store_csv_line ( PoolStringArray values, String delim="," )

   ☞ 전달한 문자열 배열을 CSV 파일 형식으로 저장. '\n' 포함
void store_line ( String line )

   ☞ 문자열과 '\n' 쓰기
void store_pascal_string ( String string )
   ☞ 파스칼 형식의 문자열(문자열 길이도 저장) 쓰기

void store_var ( Variant value, bool full_objects=false )
   ☞ 동적 변수 쓰기. 오브젝트 허용을 true로 설정하면 오브젝트도 쓸 수 있다.

void flush ( )

   ☞ 버퍼 내보내기. 닫기하면 자동으로 내보내기도 수행

 

■ 검색 및 검사

bool is_open ( ) const

   ☞ 현재 열린 상태면 true
bool eof_reached ( ) const

   ☞ 파일 끝에 도달했으면 true
Error get_error ( ) const

   ☞ 마지막 오류 코드 리턴
int get_len ( ) const

   ☞ 파일 크기 리턴
int get_position ( ) const

   ☞ 현재 파일 위치 리턴
void seek ( int position )

   ☞ 지정 위치로 이동
void seek_end ( int position=0 )

   ☞ 파일 끝으로 이동 음수 위치를 지정하면 파일 끝부터의 위치로 이동

String get_path ( ) const

   ☞ 열린 파일의 경로 리턴
String get_path_absolute ( ) const

   ☞ 열린 파일의 절대 경로 리턴

 

■ 기타 도구

String get_md5 ( String path ) const

   ☞ 지정한 경로의 파일을 대표하는 MD5 추출. 실패 시 빈 문자열
String get_sha256 ( String path ) const

   ☞ 지정한 경로의 파일을 대표하는 SHA256 추출. 실패 시 빈 문자열
int get_modified_time ( String file ) const

   ☞ 지정한 파일의 최종 변경 시간 리턴. UNIX 타임스탬프로 OS.get_datetime_from_unix_time로 변환 가능.
bool file_exists ( String path ) const

   ☞ 지정한 파일이 존재하면 true

 

728x90
댓글
글 보관함
최근에 올라온 글
최근에 달린 댓글
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31