Question: The wc command on unix/linux takes a filename as a command line parameter and prints the following counts for the contents of the file: the

The wc command on unix/linux takes a filename as a command line parameter and prints the following counts for the contents of the file: the number of newlines (' '), the number of words (contiguous non-whitespace characters), and the number of bytes.

In this lab you will write your own version of wc. Use pointers instead of brackets for all array references.

Notes

In the spirit of unix/linux, your output should be just the three counts: newlines, words, and then bytes. Separate the counts with a couple of blanks. You don't need to print any words.

Words are separated by white space characters. White space consists of the characters blank (' ', ASCII value 3210 or 2016), tab ('\t', ASCII value 910 which is also 916), and newline (' ', ASCII value 1010 or A16). Of course there can be multiple white space characters between words. All whitespace characters should be included in the byte count.

The file name is entered as a command line argument. The first element of argv is the program name, so the first command line argument is in argv[1]. Do not prompt for and read the file name! Use the parm passed to main to access the file name.

To enter command line arguments in dev-c++, choose Execute->Parameters from the menu bar. Enter the filename in the Parameters box, then click OK.

Use fopen to open the file:

FILE *fopen(const char *filename, const char *mode) 

Pass the filename as the first parm and "r" (for read mode) for the second parm. fopen returns a FILE pointer. If the file cannot be opened, fopen returns NULL.

You will read the file line by line in main, and pass each line to a function that will count the characters and the words on the line. Create an array of 201 characters to hold the input line. We will assume that each line of the file is at most 200 characters.

Use fgets to read from the file. fgets reads a line from the file into str, similar to nextLine in Java. It adds a null byte after the characters that are read. To make sure that the input doesn't overflow the array, fgets takes the size of the array as a parm. fgets stops reading when a newline is encountered, or it reads n-1 bytes, or it reaches end of file, whichever comes first. If it reads a newline it adds a null byte after the newline. If n-1 characters are read before a newline is seen, then it stops reading and adds a null byte as the nth character. If end-of-file is encountered before a newline or n-1 characters are read, fgets adds a null byte to the end of the characters read. fgets returns NULL when it reaches end of file.

 char *fgets(char *str, int n, FILE *stream); 

When you call fgets pass the string (an array of characters), the length of the array, and the file pointer returned by fopen. fopen reads Pass this string to a function which will count the number of words and characters in the string. Pass the word and character counts to the function by reference.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!