Thursday, September 11, 2014

input characters (entered by the user) into a file using fputc function


Problem

Write a program that will take characters from the keyboard, one at a time, and writes them to a file. Use fputc function.

Solution - Code

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

int main()
{
 char ch;
 FILE *ft;
 int i;

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

 while (1)
 {
  printf ("Enter Character : ");
  scanf("%c",&ch);
  fflush (stdin);
  if ( ch == '1' )
   break;
  else
   fputc (ch,ft);
 }

 fclose (ft);
 system ("pause");
 return 0;
}

Sample Output

write character by character into a file

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