Question: This assignment is an exercise in C programming, specifically in using the C library to do file input and to do some basic string processing.

This assignment is an exercise in C programming, specifically in using the C library to do file input and to do some basic string processing. It probably introduces you to at least one C library function that you have never used before. Knowing what library capability is available in any programming language's environment iskeyto using that programming language well, so here we learn a little about the C library!
Requirements
Write a plain C program that reads a plain text file and outputs the number of "words"andthe number of lines that the file contains. Your program must adhere to both the behavioral specification and the programming requirements below.
Behavioral Specification
1. The program must take either zero or one command line argument. If there is zero arguments then standard input (stdin) is used for input, otherwise the argument is the name of the input file to use. If more than one argument is given, your program must output both an error and a usage message, and then exit. If the argument given cannot be opened as a file for reading, your program must output an error message and then exit.
2. The program must output two decimal whole numbers separated by one tab character, and followed by one newline character. Nothing else. The first number is the number of "words" that the input contained, and the second is the number of lines. These numbers should match the count that the Unix command "wc" gives.
3. A word is a non-zero sequence of non-whitespace characters followed by at least one whitespace character, or EOF. Multiple whitespace characters may be in between words. Whitespace characters are space (''), tab ('\t'), newline ('
'), and carriage return ('\r'). Carriage returns generally do not occur in Unix text files, but may in Windows text files. A line is a sequence of non-newline characters followed by a newline (or EOF). A line may be empty (zero non-newline characters)-- it still counts as a line.
4. Your program must exit with a return value from main() of zero if it successfully processed its input, or -1 otherwise.
Programming Requirements
1. You must use the C library functionfgets()to read in one line at a time from input (file orstdin). You can assume that no line is longer than 1024 characters (including the newline). Since you must use fgets() to read the file, it is best to usefopen()andfclose()to open and close the file. You cannot open, and should not close,stdin.
2. You must use the C library functionstrtok()to identify words in the input. This function is meant to use repetitively in sequence on a data string. It's cool!
3. You must create a function named 'processLine()' that is called once for each line in the input; its job is to process the given input line. It can have whatever parameters and return value you deem necessary.
4. Your programmust nothave any global variables; you must use arguments and return values to communicate between functions.
5. Your program must have a file header comment, function header comments, and other comments as appropriate.
6. You must create a Makefile that compiles your program into an executable named 'hw1wc'.
7. Your Makefile must use the flag '-Wall' when it invokes the Gnu C compiler, and your program must compile with zero warnings evident.

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 Programming Questions!