Thursday, September 11, 2014

take input (from user) a poem and write it into a file using puts function


Problem

Write a C program that prompt the user to input a poem line by line. The program creates a file named poem.txt and writes that poem in that file. Use fputs function.

Solution - Code

#include<stdio.h>
#include<stdlib.h>

int main()
{
 char s[80];
 FILE *ft;
 int i;

 printf ("NOTE: If you want to exit just press enter twice.\n\n");
 ft = fopen ("Poem.txt","w");

 printf ("Enter Poem : ");
 while (1)
 {

  gets(s);

  if (strlen(s) == 0)
   break;
  else
   fputs(s,ft);
 }

 fclose(ft);
 return 0;
}

Sample Output

input poem using fputs function

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact