Write a C program named “mywc.c” simulating the “wc” command taking some options
ID: 665889 • Letter: W
Question
Write a C program named “mywc.c” simulating the “wc” command taking some options and text file as arguments. If no options are given, mywc outputs the number of lines, words and characters in the given file as well as the file name separated by space. When the –l option is used, mywc can output the number of lines; When the –w option is used, mywc can output the number of words; When the –c option is used, mywc can output the number of characters. If the given file doesn’t exist, it gives an error message with program name, text file name and the casual separated by colon. Below are sample runs of mywc(assuming mywc is the generated executable file):
$./mywc test.txt
2 5 8 test.txt
$./mywc –l test.txt
2
$./mywc –w test.txt
5
$./mywc –c test.txt
8
$./mywc –lw test.txt
2 5
$./mywc –lc test.txt
2 8
$./mywc –lwc test.txt
2 5 8
$./mywc nonexist.txt
./mywc:nonsexist.txt: No such file or directory
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//defines
#define MAXWORD 500
#define BUFFERSIZE 100
typedef unsigned long count_t;
//totals
count_t words=0;
count_t lines=0;
count_t bytes=0;
void wc(int l, int w, int c, const char *fp){
FILE *file = fopen(fp, "r");
int n;
char buffer[MAXWORD];
char ch;
count_t ccount = 0;
count_t wcount = 0;
count_t lcount = 0;
if (file != NULL){ //count newlines and total characters.
while(ch!=EOF){
ch=fgetc(file);
if(ch==' ')
lcount++;
ccount++;
}
ch=''; //stop ch from viewing the file.
fclose(file);
ccount--; //remove the EOF byte.
file = fopen(fp, "r"); //Re-open the file for scanning.
while(fscanf(file, "%s", buffer) == 1){
++wcount;
}
//Done counting. Now add the new numbers to the total count.
lines+=lcount;
words+=wcount;
bytes+=ccount;
//print according to active flags
if((l==0 && w==0 && c==0) || (l==1 && w==1 && c==1))
printf("%6lu %6lu %6lu %s ", lcount, wcount, ccount, fp);
else if(l==1 && w==0 && l==0)
printf("%6lu %s ", lcount, fp);
else if(l==0 && w==1 && c==0)
printf("%6lu %s ", wcount, fp);
else if(l==0 && w==0 && c==1)
printf("%6lu %s ", ccount, fp);
else if(l==1 && w==1 && c==0)
printf("%6lu %6lu %s ", lcount, wcount, fp);
else if(l==0 && w==1 && c==1)
printf("%6lu %6lu %s ", wcount, ccount, fp);
else if(l==1 && w==0 && c==1)
printf("%6lu %6lu %s ", lcount, ccount, fp);
}
else
{
printf("mywc: %s: No such file or directory ", fp);
}
}
//wc for stdin. slightly different behavior.
void wcf(int l, int w, int c, FILE *file){
FILE *fp = file;
int n;
char buffer[MAXWORD];
char ch;
count_t ccount = 0;
count_t wcount = 0;
count_t lcount = 0;
if (file != NULL){ //count newlines and total characters.
while(ch!=EOF){
ch=fgetc(file);
if(ch==' '){
lcount++;
}
if( (ch==' ') || (ch==' ') || (ch==' '))
wcount++;
ccount++;
}
if(wcount == 0)
wcount = 1;
ch='a'; //stop ch from viewing the file.
fclose(file);
ccount--; //remove the EOF byte.
ccount--; //remove the extra newline needed to end input in stdin.
//Done counting. Now add the new numbers to the total count.
lines+=lcount;
words+=wcount;
bytes+=ccount;
//print according to active flags
if((l==0 && w==0 && c==0) || (l==1 && w==1 && c==1))
printf("%6lu %6lu %6lu ", lcount, wcount, ccount);
else if(l==1 && w==0 && l==0)
printf("%6lu ", lcount);
else if(l==0 && w==1 && c==0)
printf("%6lu ", wcount);
else if(l==0 && w==0 && c==1)
printf("%6lu ", ccount);
else if(l==1 && w==1 && c==0)
printf("%6lu %6lu ", lcount, wcount);
else if(l==0 && w==1 && c==1)
printf("%6lu %6lu ", wcount, ccount);
else if(l==1 && w==0 && c==1)
printf("%6lu %6lu ", lcount, ccount);
}
else
{
printf("mywc: No such file or directory ");
}
}
int main(int argc, char *argv[]){
int cflag = 0;
int wflag = 0;
int lflag = 0;
int argcount = argc-1;
const char *files;
int i;
char *temp;
if(argc==1){
wcf(1,1,1,stdin);
}
else if(argc>=2)
for(i=0; i<argc; ++i){ //Activates Flags.
char *x = argv[i];
if (x[0] == '-'){
if(strlen(x)==2){ //for -l -w -c
if(x[1]=='c')
{
cflag=1;
argcount--;
}
else if(x[1]=='w')
{
wflag=1;
argcount--;
}
else if(x[1]=='l')
{
lflag=1;
argcount--;
}
}
else if(strlen(x)==3){ //2 args
if(strcmp("-lw",x)==0 || strcmp("-wl",x)==0 )
{
wflag = 1;
lflag = 1;
argcount--;
}
else if(strcmp(x,"-cw")==0 || strcmp(x,"-wc")==0)
{
wflag = 1;
cflag = 1;
argcount--;
}
else if(strcmp(x,"-cl")==0 || strcmp(x,"-lc")==0)
{
cflag = 1;
lflag = 1;
argcount--;
}
}
else if(strlen(x)==4){ //3 args
if(strcmp(x, "-lcw")==0 || strcmp(x, "-lwc")==0 || strcmp(x, "-cwl")==0 || strcmp(x,"-clw")==0 || strcmp(x,"-wcl")==0 || strcmp(x,"-wlc")==0 )
{
wflag = lflag = cflag = 1;
argcount--;
}
}
}
}
const char *filenames[argcount]; //put filenames into an array. **BUG**: cannot accept files beginning with '-'.
int arraylocation=0;
if( (strcmp(argv[0],"./mywc")==0) )
for(i=1; i<argc; i++){
char *x = argv[i];
if (x[0] != '-')
{
//printf("added %s to file list ", x);
filenames[arraylocation] = x;
arraylocation++;
}
}
for(i=0; i<argcount; i++){
//printf("", filenames[i]);
wc(lflag, wflag, cflag, filenames[i]);
}
if(argcount >1){ //printing the total, in case multiple files are accessed.
if((lflag==0 && wflag==0 && cflag==0) || (lflag==1 && wflag==1 && cflag==1))
printf("%6lu %6lu %6lu total ", lines, words, bytes);
else if(lflag==1 && wflag==0 && lflag==0)
printf("%6lu total ", lines);
else if(lflag==0 && wflag==1 && cflag==0)
printf("%6lu total ", words);
else if(lflag==0 && wflag==0 && cflag==1)
printf("%6lu total ", bytes);
else if(lflag==1 && wflag==1 && cflag==0)
printf("%6lu %6lu total ", lines, words);
else if(lflag==0 && wflag==1 && cflag==1)
printf("%6lu %6lu total ", words, bytes);
else if(lflag==1 && wflag==0 && cflag==1)
printf("%6lu %6lu total ", lines, bytes);
}
return 0;
}
test.txt
hi how r u
this is used to find word count
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.