UNIX-programming-errors

Voctl
2 min read

When an error occurs in one of the UNIX Systems functions, a negative value is often returned, and the integer 'errno' is usually set to a value that gives additional information/

for For example, the open
function returns either a non-negative file descriptor if all is OK or 1 if an error occurs. An error from open has about 15 possible errno values, such as file doesn't exist, permission problem, and so on.
Some functions use a convention other than returning a negative value. For example, most functions that return a pointer to an object return a null pointer to indicate an error.

We can see it some functions like that:

    FILE *file = fopen(argv[1] , "r");
    if (file == NULL){
        printf("cant open\n");
        return(1);}

there is a library called as "errno.h"
Errno is:


for example u can do smth like that:

    #include <errno.h> 
    #include <stdio.h>
    #include <apue.h>    
    FILE *f = fopen("something.txt", "r")
    if (f == NULL){
        printf("problem : %d\n", errno);}

so in here "errno" is give the specific error code and this error code is describing the some error (An error from open has about 15 possible errno values, such as file doesn't exist, permission problem, and so on.Some functions use a convention other than returning a negative value.)

There is some rules to respect errno like :
* If the function have error, just then use errno hm
* u cant do smth like (errno == 0) (wtf is this syntax bro)

If u want to read the error code as a human (iam not), u have some function like:

    #include <stdio.h>
    #include <string.h>
    #include <errno.h>    
    printf("%s\n", strerror(EACCES));

If u want cooler thing than these typa shits u can do something like:

#include <stdio.h>

FILE *f = fopen("bombplan.txt", "r")
if (f == NULL){
	perror("fopen");
}

so there is some example functions about this strerror and perror shits:

btw there are two types of errors:
The firs one is a fatal error. A fatal error has no recovery action. The best we can do is print an error message or do something for debugging.
The second one is a nonfatal error. Nonfatal errors can sometimes be handled more robustly.

Qutardı.

2

Responses (2)

Sign in to leave a response.

  • Voctl

    Ilk defedi bu qeder uzun blog paylasdim