# cerrno

***

**1. Checking for File Open Errors**

```cpp
#include <errno.h>
#include <cstdio>

int main() {
    FILE *fp = fopen("myfile.txt", "r");
    if (fp == NULL) {
        perror("fopen");
        return EXIT_FAILURE;
    }

    fclose(fp);
    return EXIT_SUCCESS;
}
```

**2. Handling System Call Errors**

```cpp
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

int main() {
    struct stat buf;
    if (stat("myfile.txt", &buf) < 0) {
        switch (errno) {
            case ENOENT:
                // File does not exist
                break;
            case EACCES:
                // Permission denied
                break;
            default:
                // Other error
                perror("stat");
                break;
        }
    }

    return EXIT_SUCCESS;
}
```

**3. Printing Error Messages**

```cpp
#include <errno.h>
#include <string.h>

int main() {
    char *msg = strerror(errno);
    printf("Error: %s\n", msg);

    return EXIT_SUCCESS;
}
```

**4. Getting the Error Number**

```cpp
#include <errno.h>

int main() {
    int errnum = errno;
    printf("Error number: %d\n", errnum);

    return EXIT_SUCCESS;
}
```

**5. Setting the Error Number**

```cpp
#include <errno.h>

int main() {
    errno = EINVAL;
    // ...
    return EXIT_FAILURE;
}
```

**6. Comparing Error Numbers**

```cpp
#include <errno.h>

int main() {
    if (errno == ENOENT) {
        // File does not exist
    } else if (errno == EACCES) {
        // Permission denied
    }

    return EXIT_SUCCESS;
}
```

**7. Checking for Specific Errors**

```cpp
#include <errno.h>

int main() {
    int errnum = errno;
    if ((errnum == EPERM) || (errnum == EACCES)) {
        // Permission denied
    }

    return EXIT_SUCCESS;
}
```

**8. Resetting the Error Number**

```cpp
#include <errno.h>

int main() {
    int errnum = errno;
    errno = 0;
    // ...
    if (errno != 0) {
        // An error occurred
    }

    return EXIT_SUCCESS;
}
```

**9. Checking for Errors in System Calls**

```cpp
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

int main() {
    struct stat buf;
    if (stat("myfile.txt", &buf) == -1) {
        if (errno == ENOENT) {
            // File does not exist
        } else {
            // Other error
            perror("stat");
        }
    }

    return EXIT_SUCCESS;
}
```

**10. Handling Faults**

```cpp
#include <errno.h>

int main() {
    try {
        // Code that may cause a fault
    } catch (...) {
        if (errno == EFAULT) {
            // Memory fault
        } else {
            // Other fault
            throw;
        }
    }

    return EXIT_SUCCESS;
}
```

**11. Checking for Errors in Library Functions**

```cpp
#include <errno.h>
#include <stdlib.h>

int main() {
    int *ptr = (int *)malloc(sizeof(int));
    if (ptr == NULL) {
        if (errno == ENOMEM) {
            // Out of memory
        } else {
            // Other error
            perror("malloc");
        }
    }

    free(ptr);
    return EXIT_SUCCESS;
}
```

**12. Handling Errors in Thread Functions**

```cpp
#include <errno.h>
#include <pthread.h>

int main() {
    pthread_t thread;
    if (pthread_create(&thread, NULL, myfunc, NULL) != 0) {
        if (errno == EAGAIN) {
            // Too many threads
        } else {
            // Other error
            perror("pthread_create");
        }
    }

    return EXIT_SUCCESS;
}
```

**13. Handling Errors in Socket Functions**

```cpp
#include <errno.h>
#include <sys/socket.h>

int main() {
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1) {
        if (errno == EAFNOSUPPORT) {
            // Address family not supported
        } else {
            // Other error
            perror("socket");
        }
    }

    close(sockfd);
    return EXIT_SUCCESS;
}
```

**14. Checking for Errors in File Operations**

```cpp
#include <errno.h>
#include <stdio.h>

int main() {
    FILE *fp = fopen("myfile.txt", "r");
    if (fp == NULL) {
        if (errno == ENOENT) {
            // File does not exist
        } else {
            // Other error
            perror("fopen");
        }
    }

    fclose(fp);
    return EXIT_SUCCESS;
}
```

**15. Handling Errors in Signals**

```cpp
#include <errno.h>
#include <signal.h>

int main() {
    signal(SIGINT, myfunc);
    if (errno == EINVAL) {
        // Invalid signal number
    }

    return EXIT_SUCCESS;
}
```

**16. Checking for Errors in System Calls (Symbolic Constant)**

```cpp
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

int main() {
    struct stat buf;
    if (stat("myfile.txt", &buf) == -1) {
        switch (errno) {
            case ENOENT:
            case EACCES:
                // Permission denied
                break;
            default:
                // Other error
                perror("stat");
                break;
        }
    }

    return EXIT_SUCCESS;
}
```

**17. Checking for Specific Errors (Symbolic Constant)**

```cpp
#include <errno.h>

int main() {
    if (errno == EPERM) {
        // Permission denied
    }

    return EXIT_SUCCESS;
}
```

**18. Using strerror() to Get Error Message**

```cpp
#include <errno.h>
#include <string.h>

int main() {
    char *msg = strerror(errno);
    printf("Error: %s\n", msg);

    return EXIT_SUCCESS;
}
```

**19. Checking for Errors in File Operations (Symbolic Constant)**

```cpp
#include <errno.h>
#include <stdio.h>

int main() {
    FILE *fp = fopen("myfile.txt", "r");
    if (fp == NULL) {
        switch (errno) {
            case ENOENT:
                // File does not exist
                break;
            case EACCES:
                // Permission denied
                break;
            default:
                // Other error
                perror("fopen");
                break;
        }
    }

    fclose

```
