0% found this document useful (0 votes)
33 views

Source Code of Project To Simulate The Behaviour WC Command.

This C code simulates the behavior of the Linux "wc" command, which counts the number of lines, words, and characters in a file. It takes a filename as a command line argument, opens the file, and loops through each character, incrementing counters for characters, words, spaces, and lines. It then prints a report of the totals for each category.

Uploaded by

deepika
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Source Code of Project To Simulate The Behaviour WC Command.

This C code simulates the behavior of the Linux "wc" command, which counts the number of lines, words, and characters in a file. It takes a filename as a command line argument, opens the file, and loops through each character, incrementing counters for characters, words, spaces, and lines. It then prints a report of the totals for each category.

Uploaded by

deepika
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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);
}

You might also like