cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : C Library : cstdio (stdio.h) : ferror
 
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cstdio (stdio.h)
functions:
· clearerr
· fclose
· feof
· ferror
· fflush
· fgetc
· fgetpos
· fgets
· fopen
· fprintf
· fputc
· fputs
· fread
· freopen
· fscanf
· fseek
· fsetpos
· ftell
· fwrite
· getc
· getchar
· gets
· perror
· printf
· putc
· putchar
· puts
· remove
· rename
· rewind
· scanf
· setbuf
· setvbuf
· sprintf
· sscanf
· tmpfile
· tmpnam
· ungetc
· vfprintf
· vprintf
· vsprintf
macro constants:
· EOF
· FILENAME_MAX
· NULL
· TMP_MAX
objects:
· stderr
· stdin
· stdout
types:
· FILE
· fpos_t
· size_t

-

ferror function
int ferror ( FILE * stream );
<cstdio>

Check error indicator

Checks if the error indicator associated with stream is set, returning a value different from zero if it is.
This indicator is generaly set by a previous operation on the stream that failed.

Parameters

stream
Pointer to a FILE object that identifies the stream.

Return Value

If the error indicator associated with the stream was set, the function returns a nonzero value.
Otherwise, it returns a zero value.

Example

/* ferror example: writing error */
#include <stdio.h>
int main ()
{
  FILE * pFile;
  pFile=fopen("myfile.txt","r");
  if (pFile==NULL) perror ("Error opening file");
  else {
    fputc ('x',pFile);
    if (ferror (pFile))
      printf ("Error Writing to myfile.txt\n");
    fclose (pFile);
  }
  return 0;
}

This program opens an existing file called myfile.txt in read-only mode but tries to write a character to it, generating an error that is detected by ferror.

Output:
Error Writing to myfile.txt

See also

feof Check End-of-File indicator (function)
clearerr Clear error indicators (function)
perror Print error message (function)
© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us