본문 바로가기
임베디드SW 기초

시스템 프로그래밍(2) - 파일 및 디렉토리 관련 시스템 콜

by sw-develop-record 2025. 2. 26.
  • open(): 파일을 열고 파일 디스크립터를 반환합니다.
  • close(): 파일 디스크립터를 닫습니다.
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    // 파일 열기
    int fd = open("example.txt", O_RDWR | O_CREAT, 0644);
    if (fd == -1) {
        perror("파일 열기 실패");
        return 1;
    }
    
    printf("파일 디스크립터: %d\\n", fd);
    
    // 파일 작업 수행...
    
    // 파일 닫기 - 리소스 누수 방지를 위해 필수
    if (close(fd) == -1) {
        perror("파일 닫기 실패");
        return 1;
    }
    
    return 0;
}

 


  • read(): 파일에서 데이터를 읽어옵니다.
  • write(): 파일에 데이터를 씁니다.
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>

int main() {
    int fd = open("example.txt", O_RDWR | O_CREAT, 0644);
    if (fd == -1) {
        perror("파일 열기 실패");
        return 1;
    }
    
    // 파일에 데이터 쓰기
    const char *text = "Hello, System Programming!";
    ssize_t bytes_written = write(fd, text, strlen(text));
    if (bytes_written == -1) {
        perror("파일 쓰기 실패");
        close(fd);
        return 1;
    }
    
    // 파일 포인터를 처음으로 이동
    lseek(fd, 0, SEEK_SET);
    
    // 파일에서 데이터 읽기
    char buffer[100];
    ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1);
    if (bytes_read == -1) {
        perror("파일 읽기 실패");
        close(fd);
        return 1;
    }
    
    buffer[bytes_read] = '\\0';
    printf("읽은 내용: %s\\n", buffer);
    
    close(fd);
    return 0;
}

 


  • lseek(): 파일 포인터의 위치를 변경합니다.
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

int main() {
    int fd = open("example.txt", O_RDWR);
    
    // 파일 끝으로 이동
    off_t position = lseek(fd, 0, SEEK_END);
    printf("파일 크기: %ld 바이트\\n", position);
    
    // 파일 처음으로 이동
    lseek(fd, 0, SEEK_SET);
    
    // 파일의 10번째 바이트로 이동
    lseek(fd, 10, SEEK_SET);
    
    close(fd);
    return 0;
}


  • mkdir(): 디렉토리를 생성합니다.
  • rmdir(): 디렉토리를 삭제합니다.
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

int main() {
    // 디렉토리 생성
    if (mkdir("test_dir", 0755) == -1) {
        perror("디렉토리 생성 실패");
        return 1;
    }
    printf("디렉토리 생성 성공\\n");
    
    // 디렉토리가 비어있어야 삭제 가능
    if (rmdir("test_dir") == -1) {
        perror("디렉토리 삭제 실패");
        return 1;
    }
    printf("디렉토리 삭제 성공\\n");
    
    return 0;
}


  • chdir(): 현재 작업 디렉토리를 변경합니다.
  • getcwd(): 현재 작업 디렉토리를 확인합니다.
#include <unistd.h>
#include <stdio.h>

int main() {
    char old_cwd[1024];
    
    // 현재 디렉토리 저장
    if (getcwd(old_cwd, sizeof(old_cwd)) == NULL) {
        perror("현재 디렉토리 확인 실패");
        return 1;
    }
    printf("원래 디렉토리: %s\\n", old_cwd);
    
    // 디렉토리 변경
    if (chdir("/tmp") == -1) {
        perror("디렉토리 변경 실패");
        return 1;
    }
    
    // 변경된 디렉토리 확인
    char new_cwd[1024];
    if (getcwd(new_cwd, sizeof(new_cwd)) == NULL) {
        perror("현재 디렉토리 확인 실패");
        return 1;
    }
    printf("변경된 디렉토리: %s\\n", new_cwd);
    
    return 0;
}


  • opendir(): 디렉토리를 엽니다.
  • readdir(): 디렉토리 내용을 읽습니다.
  • closedir(): 디렉토리를 닫습니다.
#include <dirent.h>
#include <stdio.h>

int main() {
    // 디렉토리 열기
    DIR *dir = opendir(".");
    if (dir == NULL) {
        perror("디렉토리 열기 실패");
        return 1;
    }
    
    // 디렉토리 내용 읽기
    struct dirent *entry;
    printf("현재 디렉토리 내용:\\n");
    while ((entry = readdir(dir)) != NULL) {
        printf("%s\\n", entry->d_name);
    }
    
    // 디렉토리 닫기 - 리소스 누수 방지를 위해 필수
    closedir(dir);
    return 0;
}


  • unlink(): 파일을 삭제합니다.
#include <unistd.h>
#include <stdio.h>

int main() {
    if (unlink("example.txt") == -1) {
        perror("파일 삭제 실패");
        return 1;
    }
    printf("파일 삭제 성공\\n");
    return 0;
}