COHERENT manpages
This page displays the COHERENT manpage for ferror() [Discover stream status].
List of available manpages
Index
ferror() -- STDIO Function (libc)
Discover stream status
#include <stdio.h>
int ferror(fp) FILE *fp;
ferror() tests the status of the file stream fp. It returns a number other
than zero if an error has occurred on fp. Any error condition that it
discovers persists until you either close the stream or call clearerr() to
clear it. For write routines that employ buffers, call fflush() before you
call ferror(), in case an error occurs on the last block written.
Example
This example reads a word from one file and writes it into another.
#include <stdio.h>
main()
{
FILE *fpin, *fpout;
int inerr = 0;
int outerr = 0;
int word;
char infile[20], outfile[20];
printf("Name data file you wish to copy:\n");
gets(infile);
printf("Name new file:\n");
gets(outfile);
if ((fpin = fopen(infile, "r")) != NULL) {
if ((fpout = fopen(outfile, "w")) != NULL) {
for (;;) {
word = fgetw(fpin);
if (ferror(fpin)) {
clearerr(fpin);
inerr++;
}
if (feof(fpin))
break;
fputw(word, fpout);
if (ferror(fpout)) {
clearerr(fpout);
outerr++;
}
}
} else {
printf
("Cannot open output file %s\n",
outfile);
exit(1);
}
} else {
printf("Cannot open input file %s\n", infile);
exit(1);
}
printf("%d - read error(s) %d - write error(s)\n",
inerr, outerr);
exit(0);
}
See Also
libc
ANSI Standard, §7.9.10.3
POSIX Standard, §8.1