Search C Program | nimishsoft@gmail.com

Count characters of file

/* Count characters of file */

#include<stdio.h>
int main()
{
 FILE *fp;
 char a[10];
 long cnt=0;
 int c;

 printf("Type a file name : ");
 gets(a);
 
 if((fp=fopen(a,"r"))==NULL)
  printf("File dosen't exist.");
 else
 {
  while(1)
  {
   c=fgetc(fp);
   if(feof(fp)) break;
   cnt++;
  }
  printf("\nfile have %ld characters",cnt);
 }
 fclose(fp);
 return 0;
}

2 comments:

  1. its wrong
    right one is here
    #include
    #include
    main()

    {

    char ch;
    int count=0;
    FILE *fptr;
    fptr=fopen("text.txt","w");
    if(fptr==NULL)
    {
    printf("File can't be created\a");
    getch();
    return 0;
    }
    printf("Enter some text and press enter key:\n");
    while((ch=getche())!='\r')
    {
    fputc(ch,fptr);
    }
    fclose(fptr);
    fptr=fopen("text.txt","r");
    printf("\nContents of the File is:");
    while((ch=fgetc(fptr))!=EOF)
    {
    count++;
    printf("%c",ch);
    }
    fclose(fptr);
    printf("\nThe number of characters present in file is: %d",count);
    getch();
    }

    ReplyDelete