Name

clearerr, feof, ferror, fileno
- check and reset stream status

Library

libc.lib

Synopsis

  #include <stdio.h>
  void clearerr (FILE *stream);
  int feof (FILE *stream);
  int ferror (FILE *stream);
  int fileno (FILE *stream);

Detailed description

The function clearerr clears the end-of-file and error indicators for the stream pointed to by stream.

The function feof tests the end-of-file indicator for the stream pointed to by stream, returning non-zero if it is set. The end-of-file indicator can only be cleared by the function clearerr.

The function ferror tests the error indicator for the stream pointed to by stream, returning non-zero if it is set. The error indicator can only be reset by the clearerr function.

The function fileno examines the argument stream and returns its integer descriptor.


Examples

/****************** this program shows finding error set using ferror **************/
/****************** and clearing it using clearerr functions ***********************/
#include <stdio.h>
int main()
{
        char a;
        FILE* fp = fopen("c:\\input.txt", "w");
        fprintf(fp, "%s", "abcdefghijklmn");
        fprintf(fp, "%c", '\n');
        fprintf(fp, "%s", "fdsfdsafsdabcdefghijklmn");
        fclose(fp);
        fp=fopen("c:\\input.txt","r");
        if (fp == NULL)
                {
                printf("fopen failed\n");
                return -1;
                }
        else
                {
                fwrite(&a, sizeof(char), 1, fp);
                if (ferror (fp))
                        printf("error set in file stream\n");
                else
                        {
                        fclose(fp);
                        return -1;
                        }
                clearerr(fp);
                if (!ferror(fp))
                        printf("error cleared in file stream\n");
                else printf("error still unexpected set in file stream\n");
                fclose (fp);
                }
        return 0;
}

         

Output

error set in file stream
error cleared in file stream

         

Errors

These functions should not fail and do not set the external variable errno.

See also

open, flockfile


Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top