File Handling in C – Set 2

In the last instructional exercise, I educated you regarding the fundamental document info yield capacities.

On the off chance that you are perusing this instructional exercise by avoiding the last instructional exercise, at that point I would firmly prescribe you to peruse that instructional exercise first. So today I will disclose to you one program that will tally every one of the characters, spaces, spaces and tabs inside one content record.

Document Handling in C

C Program to check characters, spaces, tabs and lines in a book record

Lets straight away compose the program first.

#include<stdio.h>
 
void main() 
{  
 FILE *np;  
 char ch;  
 int lines=1,tabs=0,blanks=0,characters=0; //the last line is not counted so default value of lines is 1
 np=fopen("demo.txt","r");
 
 while(ch!=EOF)  
 {   
  ch=fgetc(np);  
  characters++;  
  
  if(ch==' ')
   blanks++;  
  
  if(ch=='n')    
   lines++;  
  
  if(ch=='t')
   tabs++ ;  
 }  
 
 fclose(np); 
 printf("Characters=%d",characters);  
 printf("nSpaces=%d",blanks);  
 printf("nTabs=%d",tabs);  
 printf("nLines=%dn",lines); 
}  

Output

characters=12
spaces=1 
Tabs=0 
Lines=1
Press any key to continue...

Note: I have opened the demo.txt in my PC. So the yield will shift from record to document.

Clarification

As a matter of first importance, I have proclaimed one FILE structure pointer np and instated four whole numbers which are lines, tabs, spaces and characters.

After that I have opened the record demo.txt utilizing fopen() work. I have utilized a similar capacity in my last instructional exercise as well.

After that I have begun one while circle. Inside it I am bringing the characters of that record utilizing fgetc() work individually. This while circle will proceed till end of record. The finish of document is experienced when estimation of ch become EOF.

Presently I have utilized if squares to check the quantity of characters, spaces, lines and tabs.

Toward the finish of the circle I have shut the record utilizing fclose() work.

Then I have printed the qualities inside the whole number factors which I have used to check the characters, spaces, spaces and tabs.

Note: This is one of the most as often as possible utilized viable utilization of record information yield capacities. By presenting a few factors inside the program I have utilized it to check the characters inside the record.

Till now we have perceived how to peruse from the document and afterwards show it on support. In our next instructional exercises, we will find out about how to compose something into a record.

DURGESH

I Love python, so I like machine learning a Lot and on the other hand, I like building apps and fun games I post blogs on my website for Tech enthusiast to learn and Share Information With The World.

Leave a Reply

Your email address will not be published. Required fields are marked *

thirteen + eighteen =

Recent Content