Header file:
stdio.h
Synopsis:
int fgetc(FILE *stream);
Description:
It reads the next character from the stream and returns the integer(ascii) value of the character read.
fgetc function C example:
#include<stdio.h>
#include<errno.h>
#include<string.h>
int main() {
char ch, *str;
FILE *fp;
fp = fopen("file.txt", "r");
if (fp == NULL) {
str = strerror(errno);
perror(str);
return;
}
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
return 0;
}
#include<errno.h>
#include<string.h>
int main() {
char ch, *str;
FILE *fp;
fp = fopen("file.txt", "r");
if (fp == NULL) {
str = strerror(errno);
perror(str);
return;
}
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
return 0;
}
Output:
jp@jp-VirtualBox:~/cpgms/exp$ ls
a.out file.txt
jp@jp-VirtualBox:~/cpgms/exp$ cat file.txt
hello world
hello world
hello world
jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
hello world
hello world
hello world
a.out file.txt
jp@jp-VirtualBox:~/cpgms/exp$ cat file.txt
hello world
hello world
hello world
jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
hello world
hello world
hello world
No comments:
Post a Comment