Pages

Count blanks in a file

Write a C program that counts the number of  blanks in a text file using standard I/O


#include <fcntl.h>
#include < sys/stat.h>
#include <stdio.h>
int main(int argc, char **argv)
{
   FILE *fd1;
   int n,count=0;
   char buf;
   fd1=fopen(argv[1],"r");
   while(!feof(fd1))
   {
   buf=fgetc(fd1);
     if(buf==' ')
         count=count+1;
   }
   printf("\n Total Blanks= %d",count);
return (0);
}

-------------------------------------------------


Write a C program that counts the number of blanks in a text file using system calls


#include<fcntl.h>
#include<sys/stat.h>

int main(int argc, char **argv)
{
   int fd1;
   int n,count=0;
   char buf;
   fd1=open(argv[1],O_RDONLY);
   while((n=read(fd1,&buf,1))>0)
   {
     if(buf==' ')
         count=count+1;
   }
   printf("\n Total Blanks= %d",count);
return (0);
}

If you like this please Link Back to this article...



0 comments:

Post a Comment