fopen 함수를 종종 쓸일이 생기는데, 매번쓰는 옵션값 외에는 헷갈리기에 정리해 보았다.
fopen 함수는 파일 입출력을 위한 함수로 다음과 같이 사용할 수 있다.
FILE *fp = NULL; //우선 파일 포인터로 사용할 변수를 선언하고, //경로와 모드값으로 파일을 오픈한뒤 파일포인터에 넣는다. //fopen 함수의 형태는 다음과 같다. //FILE *fopen(const char *filename, const char *mode); fp = fopen("test.txt", "wb");
그리고 위에 fopen Mode 값으로 사용할 수 있는 값들은 다음과 같다.
r : Open for reading only. (읽기 전용)
w : Create for writing. If a file by that name already exists, it will be overwritten.
(쓰기용으로 파일생성, 동일이름이 있다면 덮어씀.)
a : Append; open for writing at end-of-file or create for writing if the file does not exist.
(동일명 파일 존재시 끝에 이어씀, 동일명 파일 없으면 쓰기용 파일 생성)
r+ : Open an existing file for update (reading and writing).
(현재 있는 파일에 read/write 모두 덮어씀.)
w+ : Create a new file for update (reading and writing).
If a file by that name already exists, it will be overwritten.
(읽기/쓰기용으로 파일 생성. 같은 파일 존재시 덮어)
a+ : Open for append; open (or create if the file does not exist) for update at the end of the file.
(파일이있으면 뒤에 추가, 업다면 새로 생성)
t : Text Mode (텍스트 모드)
b : Binary Mode (이진 파일 모드)
'Computer Programming > C,C++,MFC' 카테고리의 다른 글
[C/C++] C언어 입력함수 scanf (0) | 2017.02.15 |
---|---|
[C/C++] Bit Field, 비트 변수 ':' (0) | 2017.02.13 |
[C/C++] bsearch 함수 (0) | 2016.06.22 |
[MFC]CString <-> Char* 변환 (0) | 2013.11.08 |
[C/C++]OpenSSL Library 사용을 위해 (0) | 2013.10.15 |