ferror - C stdio.h

C examples for stdio.h:ferror

Type

function

From


<cstdio>
<stdio.h>

Prototype

int ferror ( FILE * stream );

Description

Checks if the error indicator, returning a value different from zero if it is.

Parameters

ParameterDescription
stream Pointer to a FILE object that identifies the stream.

Return Value

A non-zero value is returned if the error indicator is set. Otherwise, zero is returned.

Example

The following code shows how to open an existing file in read-only mode and tries to write a character to it.

It will generate an error that is detected by ferror.

Demo Code


#include <stdio.h>
int main ()//  www .j  ava2s  . c  o  m
{
  FILE * pFile;
  pFile=fopen("main.cpp","r");
  if (pFile==NULL) {
     perror ("Error opening file");
     return -1;
  }

  fputc ('x',pFile);
  if (ferror (pFile))
      printf ("Error Writing to main.cpp\n");
  fclose (pFile);

  return 0;
}

Related Tutorials