Sunteți pe pagina 1din 2

Source code of project to simulate the behaviour wc

command..
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include <stdio.h>
Int main(int argc , char *argv[])
{
FILE *fp;
int ch;
int chr=0;
int totchr=0;
int bspc=0,totbspc=0;
int nline=0;
int word=0,totwrd=0;
int i=0;
fp=fopen(argv[1],"r");
if(argc != 2)
{
printf("\tInsufficient arguments\n");
printf("\tusage: wrd <harshit>\n");
exit(0);
}
if(fp==NULL)
{
printf("Error In File Opening\n");
exit(0);
}
else
{
while((ch=fgetc(fp))!=EOF)
{
if(ch == ' ')
{
bspc++;
}
if(ch == '\n')
{
nline++;
}
chr++;
word++;

}
};
totchr += chr;
totbspc += bspc;
totwrd += word;
printf("\nFile %s has\n", argv[1]);
printf("\n\twhite spaces are: %d\n", totbspc);
printf("\twords are: %d\n", totwrd);
printf("\tcharacters are: %d\n", totchr);
printf("\tlines are: %d\n\n", nline);
fclose(fp);
}

S-ar putea să vă placă și