Wednesday, 23 April 2014

31. Write a program to create a file, open it, type-in some characters and count the number of characters in a file.



#include <stdio.h>
#include<conio.h>
#include<string.h> main()
{
char ch;
int count=0; FILE *fp; clrscr();
if ( (fp = fopen("vim","w")) == NULL)
{
fprintf(stderr, "Cannot open input file.\n"); getch();
return 1;
}
while ( ( ch =getchar()) != EOF )

{
putc(ch,fp); switch(ch)
{
case \n:
case \t:
case :break; default : count++; break;
}
}
printf("no of characters: %d\n", count); fclose(fp);
getch();
return 0;
}

No comments:

Post a Comment