Stream Errors

来源:互联网 发布:泛海三江主机编程视频 编辑:程序博客网 时间:2024/06/06 15:53

To indicate an error, many stdiolibrary functions return out-of-range values, such as null pointers or the constant EOF. In these cases, the error is indicated in the external variable errno:

#include
extern int errno;

Note that many functions may change the value of errno. Its value is valid only when a function has failed. You should inspect it immediately after a function has indicated failure. You should always copy it into another variable before using it, because printing functions, such as fprintf, might alter errnothemselves.


You can also interrogate the state of a file stream to determine whether an error has occurred, or the end of file has been reached.


#include
int ferror(FILE *stream);
int feof(FILE *stream);
void clearerr(FILE *stream);


The ferrorfunction tests the error indicator for a stream and returns nonzero if it’s set, but zero otherwise.The feoffunction tests the end-of-file indicator within a stream and returns nonzero if it is set, zero oth-erwise. Use it like this:


if(feof(some_stream))
/* We’re at the end */


The clearerrfunction clears the end-of-file and error indicators for the stream to which streampoints.It has no return value and no errors are defined. You can use it to recover from error conditions on streams. One example might be to resume writing to a stream after a “disk full” error has been resolved.

原创粉丝点击