This blog is under construction

Friday 27 April 2012

getc example in C

Header file:
    stdio.h

Synopsis:
     int getc(FILE *stream);

Description:
    Reads the next character from the given stream. It evaluates the given stream more than one time, if it is a macro.  Returns integer value(ascii) of the character read.


getc 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 = getc(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



No comments:

Post a Comment