Handling errors in C language

H

The C language was created in the early 1970s and was originally intended to write part of the Unix operating system. C language is a universal programming language, characterized by a concise expression, a modern control of the execution flow, data structures, and a rich set of operators.
The C Language is not a “very high level” language and is not specialized for a particular field of applications. The absence of restrictions and its generality make it a more convenient and effective language than many other stronger languages.

The C Language allows writing well-structured programs, due to its flow control constructs: instruction groups, decision making (if), cycles with the termination test before the cycle (while, for) or after the cycle (do) and the selection of a case from a lot of cases (switch).

Although the C Language is therefore a relatively low level language, it is a pleasant, expressive and elastic language that is suitable for a wide range of programs. C is a restricted language and is learned relatively easily, and subtleties are retained as the programming experience grows.

perror – displays a system error message

Statement: void perror (const char *s);

#include <errno.h>
const char *sys_errlist [];
int sys_nerr;

The perror routine displays a message at the standard error output, describing the last error encountered at the last system call or library function. First the argument s is displayed, then the comma and blank, and finally the error message and the new-line. It is recommended (especially for troubleshooting) that the argument s include the name of the function in which the error occurred. The error code is taken from the external variable errno.

The global error list sys_errlist [] indexed with errno can be used to get the error message without newline. The last message index in the list is sys_nerr-1. Particular attention is recommended when accessing the list directly because some new error codes may be missing from sys_errlist [].
If a system call fails the errno variable indicates the error code. These values ​​can be found in <errno.h>. The dog function is used to display this error code in a readable form. If an error-terminated call is not immediately followed by a per-call, the value of the errno variable may be lost if not saved.

clearerr, feof, ferror – checks and resets the status flow

Statement: void clearerr (FILE *flow);
int feof (FILE *flow);
int ferror (FILE *flow);
int filen (FILE *flow);

• The clearerr function clears the end of file and flow error indicators.
• The feof function tests the end-of-file indicator for the stream, and returns non-zero if set. This is set if a read operation has detected the end of the file.
• The ferror function tests the flow error indicator, and returns non-zero if set. This is set if a read or write operation has detected an error (due to hardware for example).
• The read functions (with or without format) make no distinction between end of file and error, so the feof and ferror functions must be called to determine the cause.
• The fileno function examines the flow argument and returns the descriptor associated with the operating system for this flow.

Atention! It is very common to use the feof function incorrectly to test if the end of the file has been reached. In this case, this style of programming is not recommended:

#define LSIR 80
char lin [LSIR];
FILE *fi, *fo;
fi=fopen (filename-entry,”rt”);
fo=fopen (filename-out,”wt”);
while (! feof(fi)) {/ *wrong! */
fgets (lin,LSIR,fi);
fputs (lin,fo);
}
fclose(fi);fclose(fo);

In this sequence, if even the last line of the input text file is terminated with new-line, it will be written twice in the output file. Why? After the last line is read, the end of file indicator is still not positioned, so the fgets function returns successful. When resuming the cycle, a new fgets is tried and only now is the end of the file detected, a fact marked in the area reserved for the flow fi. Thus the content of the smooth array remains unchanged and is written a second time in the output file. It is only at a new resumption of the cycle that the feof function tells us that the end of file has been detected.

Recent Posts

Archives

Categories