Completely Solved C, C++ Programs Assignment.




C program to count total number of lines, words and characters in a file

Filed Under:

Program Statement :
Write a C program to count total number of lines, words and characters in a file. The following operations must be supported
1. -l (count only number of lines),
2. -w (count only number of words),
3. -c(count only characters),
which must be passed as command line arguments.

Theory :
A file is a collection of bytes stored on a secondary storage device, which is generally a disk of some kind. A text file can be a stream of characters that a computer can process sequentially. Every file being opened for any operations like :
 "r" - Read-Only mode or "r+"- read+write mode.
 "w" - Write-Only mode "w+" - write+read mode.
 "a" - Append mode "a+" - read+append mode.
For implementing this program we have to implement wc (short for word count) command in UNIX operating system. It generally counts number of lines, number of words, number of characters of a file as shown :
wc print the line count, print the character count, print the word count
With particular options it can also make a selective count as shown below :
1. wc -l ; print the line count
2. wc -c ; print the character count
3. wc -L ; print the length of longest line
4. wc -w ; print the word count
This command works successfully for multiple files also. In that case total number of lines, words and characters of two files are also printed. Here,we implement wc command and also implement selective options wc -l, wc -w, wc -c according to the given problem. We do not implement wc -L and also do not implement wc for multiple files as that are not given in the problem. Implementation is done successfully taking inputs as command line arguments.

Algorithm :
main(int argc, char* argv[])
{
FILE* fp; /*Declaring a file pointer*/
if(argc<2) /*Insufficient no. of arguments given*/
{
print(COMMAND ERROR);
End Algorithm;
}
if(argc>3)/*Too many arguments given*/
{
printf(COMMAND ERROR);
End Algorithm;
}
if(argc==3)
{
fp = fopen(argv[2],"r");
if(fp==NULL) /*File does not exist or it cannot be opened successfully*/
{
printf(ERROR in opening file);
End Algorithm;
}
}
else
{
fp = fopen(argv[1],"r");
if(fp==NULL) /*File does not exist or it cannot be opened successfully*/
{
printf(ERROR in opening file);
End Algorithm;
}
}
ch = Read a character from the input file;
while(End of file is not encountered)
{
if(ch==' ')
{
space = 1;
}
else
if(ch=='n')
{
l=l+1;
space = 1;
}
else
{
if(space==1)
w = w+1;
space = 0;
}
c = c+1;
ch = Read a character from the input file ;
}
fclose(fp); /*Close the file*/
}

Program listing :
/* C program to implement wc command in unix */
#include<stdio.h>
#include<string.h>
main(int argc,char* argv[])
{
FILE* fp;/*Declaring a file pointer*/
int w=0, c=0, l=0, space=1;
char ch;
if(argc<2)/*Insufficient no. of arguments given*/
{
printf("COMMAND ERRORn");
return;
}
if(argc>3)/*Too many arguments given*/
{
printf("COMMAND ERRORn");
return;
}
if(argc==3)
{
fp = fopen(argv[2],"r");
if(fp==NULL)/*File does not exist or it cannot be opened successfully*/
{
printf("ERROR in opening filen");
return;
}
}
else
{
fp=fopen(argv[1],"r");
if(fp==NULL)/*File does not exist or it cannot be opened successfully*/
{
printf("ERROR in opening filen");
return;
}
}
ch=fgetc(fp);
while(!feof(fp))
{
if(ch==' ')
{
space=1;
}
else
if(ch=='n')
{
l++;
space=1;
}
else
{
if(space==1)
w++;
space=0;
}
c++;
ch=fgetc(fp);
}
/*Checking which information user wants and then print the corresponding info. along with the filename*/
if(argc==3)
{
if(!strcmp(argv[1],"-w"))
printf("%3d",w);
else if(!strcmp(argv[1],"-l"))
printf("%3d",l);
else if(!strcmp(argv[1],"-c"))
printf("%3d",c);
printf(" %sn",argv[2]);
}
else/*Print number of lines,words and characters of the file along with the filename*/
{
printf("%3d",l);
printf("%3d",w);
printf("%3d",c);
printf(" %sn",argv[1]);
}
fclose(fp);/*Close the file*/
}

Output :
[user@localhost ~]$ gcc wc.c
[user@localhost ~]$ cat>abc.txt
Hi any one there??.............
Can any one hear me??
..
..
..
I am a student.
I am in 2nd year now.
I love to play computer games.^Z
[1]+ Stopped cat > abc.txt
[user@localhost ~]$ ./a.out abc.txt
7 22 101 abc.txt
[user@localhost ~]$ ./a.out -w abc.txt
22 abc.txt
[user@localhost ~]$ ./a.out -l abc.txt
7 abc.txt
[user@localhost ~]$ ./a.out -c abc.txt
101 abc.txt

Discussions :
 The variable argc is an argument counter that counts the number of arguments on the command line. The argv is an argument vector and represents an array of character pointers that point to the command line arguments. The size of the array will be equal to argc. For the command line given above, argc is three and argv is an array of three pointers to string.
 The argument count should not exceed 3 and also should not less than 2. If it does, an error message is displayed.
 "fclose" flushes any buffers maintained for the given file and closes the file. Buffers allocated by the standard I/O system are freed.



Back to main directory:  C++ Assignment    Software Practical


Get Free Programming Tutorials and Solved assignments