동캄의 코딩도장
DISK I/O [2] 본문
Standard IO
- A platform-independent, user-buffering solution
File pointer
- File operation을 관리하는 구조체(FILE)을 가리키는 포인터
- 내부적으로 file descriptor와 연동(mapping)됨
Stream
- 프로그램과 file을 연결한 통로
Workflow of file I/O
File open -> File access (Read/ Write) -> File close
FIle open (Opening a file/stream)
#include <stdio.h>
FILE *fopen (const char *path, const char *mode);
path (file path)
- 열려는 파일의 경로
mode (file open mode)
- 파일 열기 모드
Return: file pointer (NULL: fail to open)
파일열기모드(mode)
'r' 읽기
'w' 쓰기
'a' 덧붙이기
'r+' 읽기+ 모드 / 쓰기 모드로 전환 가능
'w+' 쓰기 + 모드/ 읽기 모드로 전환 가능
'a+' 덧붙이기+/읽기 모드로 전환 가능
'b' 이진(binary) 파일모드
Ascii(text) fiel & Binary file
Ascii(text) file (텍스트 파일)
- 문자들이 들어있는 표시 -> 사람이 읽을 수 있는 형태
- 각 문자는 ascii 코드로 표현
Binary file (이진 파일)
- 이진 데이터가 직접 저장됨-> 사람이 그대로 읽기 어려운 형태 ( 실행 파일, 사운드 파일, 이미지 파일 등)
- 줄(line)로 구분되지 않는, 이진 데이터의 연속
- 컴퓨터가 읽을 수 있는 형태 -> 데이터를 효율적으로 다룰 수 있음
Closing a files/streams
#include <stdio.h>
int fclose (FILE *stream);
stream
- 닫으려는 stream
Return ( 0: success, -1(EOF): error)
'CS > 시스템프로그래밍' 카테고리의 다른 글
DISK I/O [4] (0) | 2022.02.07 |
---|---|
DISK I/O [3] (0) | 2022.02.07 |
DISK I/O [1] (0) | 2022.02.04 |
FILE I/O [3] (0) | 2022.02.04 |
FILE I/O [2] (0) | 2022.02.04 |