Notice
Recent Posts
Recent Comments
Link
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Archives
Today
Total
04-29 19:41
관리 메뉴

nomad-programmer

[Programming/C] errno.h 헤더 파일 본문

Programming/C

[Programming/C] errno.h 헤더 파일

scii 2020. 6. 19. 03:20

errno 변수는 errno.h 헤더 파일에 정의된 전역 변수이다. 이 변수에는 다음과 같이 표준적으로 정의된 에러 값이 저장된다.

EPERM=1

허용되지 않은 연산

ENOENT=2

지정한 파일이나 디렉토리가 없다

ESRCH=3

지정한 프로세스가 없다

이 값으로 errno 변수를 검사할 수 있다. 아니면 string.h 헤더 파일에 정의도니 strerror() 함수를 사용해 표준 에러 메시지를 만들 수 있다.

// strerror() 함수는 에러 번호를 메시지로 변환한다.
puts(strerror(errno));

따라서 시스템이 실행하려는 프로그램을 찾지 못하면 errno 변수를 ENOENT로 설정하며, 이 번호는 다음과 같은 표준 에러 메시지에 대응된다.

No such file or directory

 

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

int main(void) {
    if(-1 == execlp("test5", "test5", NULL)){
        fprintf(stderr, "test5를 실행할 수 없음: %s\n", strerror(errno));
        return -1;
    }
    return 0;
}

// 결과
/*

test5를 실행할 수 없음: No such file or directory

*/
Comments