Ferror( ) function determines whether a file operation has produced an error. - C File

C examples for File:File Operation

Introduction

The ferror( ) function has this prototype,

int ferror(FILE *fp);

where fp is a valid file pointer.

It returns true if an error has occurred during the last file operation; otherwise, it returns false.

Each file operation sets the error condition, and ferror( ) should be called immediately after each file operation; otherwise, an error may be lost.

The program substitutes spaces for tabs in a text file and supplies error checking.

Demo Code

#include <stdio.h>
#include <stdlib.h>

#define TAB_SIZE 8//from  w  w w. j  a  va  2  s  .co m
#define IN 0
#define OUT 1

void err(int e);

int main(int argc, char *argv[])
{
   FILE *in, *out;
   int tab, i;
   char ch;

   if (argc != 3) {
      printf("usage: detab <in> <out>\n");
      exit(1);
   }

   if ((in = fopen(argv[1], "rb")) == NULL) {
      printf("Cannot open %s.\n", argv[1]);
      exit(1);
   }

   if ((out = fopen(argv[2], "wb")) == NULL) {
      printf("Cannot open %s.\n", argv[1]);
      exit(1);
   }
   tab = 0;
   do {
      ch = getc(in);
      if (ferror(in)) err(IN);

      /* if tab found, output appropriate number of spaces */
      if (ch == '\t') {
         for (i = tab; i<8; i++) {
            putc(' ', out);
            if (ferror(out)) err(OUT);
         }
         tab = 0;
      }
      else {
         putc(ch, out);
         if (ferror(out)) err(OUT);
         tab++;
         if (tab == TAB_SIZE) tab = 0;
         if (ch == '\n' || ch == '\r') tab = 0;
      }
   } while (!feof(in));
   fclose(in);
   fclose(out);

   return 0;
}

void err(int e)
{
   if (e == IN) printf("Error on input.\n");
   else printf("Error on output.\n");
   exit(1);
}

Result


Related Tutorials