Contact Learn C
Copy Program Share Program

Program 206:To print number of Spaces,lines,characters and Tabs in a File

Program 206:
 
#include<stdio.h>
#include<stdlib.h>
 main ()
 {
    FILE *file;
    char c;
    int tabs=0,lines=0,spaces=0,charcters=0;
    file=fopen("file123.txt","r");
    printf("The file contains the following data\n");
    printf("\n");
    while(1)
    {
     if(file==NULL)
     {
      printf("File Not Found\n");
      exit(0);
     }
     else
     {
      c=fgetc(file);
      if(c==EOF)
      {
       break;
      }
      printf("%c",c);
     }
    }
    fclose(file);
    file=fopen("file123.txt","r");    
    while(1)
    {
     if(file==NULL)
     {
      printf("File Not Found\n");
      exit(0);
     }
     else
     {
      c=fgetc(file);
      if(c==EOF)
      {
       break;
      }
      if(c==' ')
      {
       spaces++;
      }
      if(c=='\n')
      {
       lines++;
      }
      if(c=='\t')
      {
       tabs++;
      }
      charcters++;
     }
    }
    fclose(file);
    
    printf("No of Charcters=%d\n",charcters);
    printf("No of Spaces=%d\n",spaces);
    printf("No of Lines=%d\n",lines);
    printf("No of Tabs=%d\n",tabs);    
 }
Explanation:

//Coming Soon...

Output:

Store both text file and program in same folder.Here I named text file as file123.txt

Running the Program

To print number of Spaces,lines,characters and Tabs in a File





Text in File




 

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide