This blog is under construction

Thursday, 5 April 2012

fclose example in C

Header file:
     stdio.h

Synopsis:
            int fclose(FILE *fp);

Description:
       Flushes the stream pointed to by fp and closes the stream fp.  Returns 0 on success, otherwise EOF is returned.


fclose function C example:


  #include<stdio.h>
  #include<errno.h>
  #include<string.h>
  int main() {
        FILE *fp;
        char ch, *str;
        fp = fopen("file.txt", "r");
        if (fp == NULL) {
                str = strerror(errno);
                /* prints error message corresponding to errno */
                perror(str);
                return;
        }
        while ((ch = getc(fp)) != EOF)
                putchar(ch);
        fclose(fp);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ cat file.txt
  hello world
  hello world
  jp@jp-VirtualBox:~/$ ./a.out
  hello world
  hello world


No comments:

Post a Comment