User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:stdio.h:ferror

ferror

    int ferror(FILE *stream);

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. Returns non-zero if error indicator is set for stream stream.

C Sourcecode Example

/* 
 * ferror example code
 * http://code-reference.com/c/stdio.h/ferror
 */
 
#include <stdio.h> /* including standard library */
//#include <windows.h> /* uncomment this for Windows */
 
 
int main( void )
{
  FILE *handle;
  handle = fopen("test.txt","r");
 
  if (handle!=NULL) {    
    printf ("its all fine\n");
    putc('X',handle);
    if (ferror(handle)) { printf ("error in writing to file\n");} 
    fclose (handle);
  } else {
    perror ("error opening file for reading\n");
  }
  return 0;
}

ferror output

if the test.txt does not exist

  output: ./ferror 
  error opening file for reading
  : No such file or directory

if the test.txt exists

  output: ./ferror 
  its all fine
  error in writing to file
  

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
c/stdio.h/ferror.txt · Last modified: 2024/02/16 01:05 (external edit)

Impressum Datenschutz