Consider the code below, which represents a simplified version of the wc (word count) Linux command. The code below displays the number of characters in a file.
a) Modify the code below to also display the number of words and lines in the file.
Define a word as any sequence of alphanumeric characters that does not contain whitespace characters such as space, tab, or newline.
By default, a line is any sequence of characters terminated by a newline.
To run this program from the shell prompt, it should look like "$ wc1 file1"
/* wc1 -- counts the characters in a file */
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFSIZE 512
int main(int argc, char * argv[]) {
char buffer[BUFSIZE];
int filedes;
ssize_t nread;
long total = 0;
/* open argv[1] read only */
if ((filedes = open(argv[1], O_RDONLY)) == -1) {
printf("error in opening anotherfile\n");
exit(1);
}
}