C program Receives strings from keyboard and writes them to file

C program Receives strings from keyboard and writes them to file

#include "stdio.h" 
main( ) 
{ 
 FILE *fp ; 
 char s[80] ; 
fp = fopen ( "POEM.TXT", "w" ) ; 
 if ( fp == NULL ) 
 { 
 puts ( "Cannot open file" ) ; 
 exit( ) ; 
 } 
 printf ( "\nEnter a few lines of text:\n" ) ; 
 while ( strlen ( gets ( s ) ) > 0 ) 
 { 
 fputs ( s, fp ) ; 
 fputs ( "\n", fp ) ; 
 } 
 
 fclose ( fp ) ; 
}

And here is a sample run of the program…

Enter a few lines of text:
Shining and bright, they are forever,
so true about diamonds,
more so of memories,
especially yours !

Leave a Comment