Header file:
stdio.h
Synopsis:
int ferror(FILE *stream);
Description:
Returns 0, if there's no error. Non-zero is returned if there's any error indicator.
ferror function C example:
#include<stdio.h>
#include<string.h>
#include<errno.h>
int main() {
FILE *fp;
int ret;
char ch, *str;
fp = fopen("ferror.txt", "r");
if (fp == NULL) {
str = strerror(errno);
perror(str);
return;
}
fputc('a', fp);
ret = ferror(fp);
if (ret) {
printf("Tried to write in a file which is opened in read mode\n");
}
fclose(fp);
return 0;
}
#include<string.h>
#include<errno.h>
int main() {
FILE *fp;
int ret;
char ch, *str;
fp = fopen("ferror.txt", "r");
if (fp == NULL) {
str = strerror(errno);
perror(str);
return;
}
fputc('a', fp);
ret = ferror(fp);
if (ret) {
printf("Tried to write in a file which is opened in read mode\n");
}
fclose(fp);
return 0;
}
Output:
jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
Tried to write in a file which is opened in read mode
jp@jp-VirtualBox:~/cpgms/exp$ cat ferror.txt
Sample program for ferror
1st open file in read mode
Try to write in it
ferror returns non-zero value
since we are trying to write in a file which is opened in read mode
Tried to write in a file which is opened in read mode
jp@jp-VirtualBox:~/cpgms/exp$ cat ferror.txt
Sample program for ferror
1st open file in read mode
Try to write in it
ferror returns non-zero value
since we are trying to write in a file which is opened in read mode
Do you need Finance? Are you looking for Finance? Are you looking for finance to enlarge your business? We help individuals and companies to obtain finance for business expanding and to setup a new business ranging any amount. Get finance at affordable interest rate of 3%, Do you need this finance for business and to clear your bills? Then send us an email now for more information contact us now via (financialserviceoffer876@gmail.com) whats-App +918929509036 Dr James Eric Finance Pvt Ltd Thanks
ReplyDelete