본문 바로가기
HOME

LinuxSP - FILE I/O: 함수

by mingnei__ 2025. 4. 24.

open(2)

파일을 오픈하거나 생성할 때 사용하는 시스템 호출 함수(2)이다.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int oflag, ... /* mode_t mode */);

/* return: 파일 열기 성공 → 파일 디스크립터 번호 / 에러 → -1 */​

* oflag arguments

O_RDONLY open for reading only
O_WRONLY open for writing only
O_RDWR open for reading and writing
O_APPEND The file is opened in append mode.
O_CREAT If pathname does not exist, create it as a regular file.
O_EXCL Ensure that this call creates the file:
if this flag is specified in conjunction with O_CREAT, and pathname already exists, then open() fails with the error EEXIST.
O_TRUNC If the file already exists and is a regular file and the access mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0.
O_NOCTTY If pathname refers to a terminal device—see tty(4)—it will not become the process's controlling terminal even if the process does not have one.
S_IRWXU  00700 user (file owner) has read, write, and execute permission
S_IRUSR 00400 user has read permission
S_IWUSR 00200 user has write permission
S_IXUSR 00100 user has execute permission
S_IRWXG 00070 group has read, write, and execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 others have read, write, and execute permission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
    rwxrwxrwx

* 에러 처리 (open())

if (open(fname, O_RDWR) < 0) {
	fprintf(stderr, "open error: %s", fname);
    exit(1);
}

 

close(2)

오픈한 파일을 닫을 때 사용하는 시스템 호출 함수(2)이다.

#include <unistd.h>
int close(int filedes);

/* return: 파일 닫기 성공 → 0 / 에러 → -1 */

 

creat(2)

파일을 생성하는 시스템 호출 함수(2)이다.

#include <fcntl.h>
#inlcude <sys/stat.h>
#include <sys/types.h>
int creat(const char *pathname, mode_t mode);

/* return: 파일 생성 성공 → 쓰기 전용으로 열린 파일 디스크립터 / 에러 → -1 */

파일이 쓰기 전용으로만 열리기 때문에 파일을 닫고 다시 원하는 모드로 열어줘야 한다.

* 애초부터 open으로 여는 것도 좋은 선택지

/* 완전히 같음 */
creat(pathname, mode); open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
/* 쓰기까지 가능하게 해줌 */
open(pathname, O_RDWR | O_CREAT | O_TRUNC, mode);

 

* 이미 존재하는 파일에 x 권한이 없을 때, TRUNC에 실패하여 파일 생성에 실패하게 된다.

* mode에 쓰기 권한을 주지 않더라도 새로 연 파일에 내용을 작성할 수 있다.

 

lseek(2)

오픈된 파일의 오프셋 위치를 명시적으로 변경할 때 사용하는 시스템 호출 함수(2)이다.

#include <unistd.h>
#include <sys/types.h>
off_t lseek(int filedes, off_t offset, int whence);

/* return: 성공 → 새로운 파일 오프셋 / 에러 → -1 */
whence → SEEK_SET SEEK_CUR (offset: 음수 가능) SEEK_END

 

write(2)

#include <unistd.h>
ssize_t write(int filedes, const void *buf, size_t nbytes);

/* return: 성공 → 기록한 바이트 수 / 에러 → -1 */

 

read(2)

#include <unistd.h>
ssize_t read(int filedes, void *buf, size_t nbytes);

/* nbytes 만큼 읽어오고 싶다는 뜻 */
/* return: 읽어온 바이트 수 / 파일의 끝 도달 → 0 / 에러 → -1 */

 

pread(2) / pwrite(2)

#include <unistd.h>
ssize_t pread(int filedes, void *buf, size_t nbytes, off_t offset);

ssize_t pwrite(int filedes, void *buf, size_t nbytes, off_t offset);

 

dup(2), dup2(2)

 

sync(2), fsync(2), fdatasync(2)