This blog is under construction

Friday 27 April 2012

puts example in C

Header file:
    stdio.h

Synopsis:
     int puts(const char *str);

Description:
     It writes the given string and a newline to stdout.  Returns non-negative integer on success.  Otherwise, EOF is returned.


puts function C example:


  #include<stdio.h>
  #include<string.h>
  int main() {
        char str[100];
        FILE *fp;
        fp = fopen("mastering-c.txt", "r");
        printf("File contents:");
        fgets(str, 100, fp);
        while(!feof(fp)) {
                str[strlen(str) - 1] = '\0';
                puts(str);
                fgets(str, 100, fp);
        }
        fclose(fp);
        return 0;
  }



   Output:
  jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
  File contents:Hello world
  This is puts example program in C
  Thanks for visiting our blog

  jp@jp-VirtualBox:~/cpgms/exp$ cat mastering-c.txt
  Hello world
  This is puts example program in C
  Thanks for visiting our blog




No comments:

Post a Comment