fopen 파일 열기 모드 옵션 정리

by digipine posted Nov 02, 2017
?

Shortcut

PrevPrev Article

NextNext Article

ESCClose

Larger Font Smaller Font Up Down Go comment Print

fopen의 파일 열기 옵션은 자꾸 잊어먹어서 그때 그때 찾이 보기도 불편해서 정리해 보았다.


"r" : 읽기 전용 모드. 파일이 없으면 NULL return.
"w" :쓰기 전용 모드. 파일이 없으면 생성되고 있으면 내용이 없어진다.
"a" : append모드. 파일이 없으면 생성. 이미 존재하는 파일 끝부분에 file pointer가 위치하게 되며 이 위치부터 뒷쪽으로만 write가능. 읽기는 불가능. fseek등으로 이 부분보다 앞으로 file pointer를 이동시키면 어떻게 될까 ? 아래 내용으로 봐서는 fseek등으로 file pointer를 이동하여도 이와 상관없이 파일 끝부분에 write가 되는 것으로 생각됨.

Opening a file in append mode (a in the mode) causes all subsequent writes to the file to be forced to the current end-of-file, regardless of previous calls to the fseek() function.

"r+" : 읽고 쓰기 모드, 파일이 없으면 NULL return.
"w+" : 읽고 쓰기 모드 단, 파일이 없으면 만들고 있으면 기존 내용을 지움. write를 먼저 한 후 동일 파일 포인터로 읽기 수행이 필요한 경우 사용. 보통은 읽기 전용, 혹은 쓰기 전용으로 fopen하므로 w+가 필요한 일은 별로 없을 듯.
"a+" : append모드, 읽고 쓰기 가능. 파일이 이미 존재할 경우 그 파일의 끝부분에서부터 추가된 내용을 쓴다. 읽기는 fseek로 지정한 file pointer위치에서 가능하나 쓰기는 파일 끝부분에서만 가능.

When a file is opened with update mode (+ in the mode), both input and output may be performed on the associated stream

TAG •