Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include #include #include /* * Expected usage: * ./wc * * If argv[1] is words, then you should count the number of

#include
#include
#include
#include
#include
#include
/*
* Expected usage:
* ./wc
*
* If argv[1] is "words", then you should count the number of words. If it
* is "lines", then you should count the number of lines.
*
* For example:
* $ cat a.txt
* a b c d
* $ ./wc words a.txt
* 4
* $ ./wc lines a.txt
* 1
*
* YOUR PROGRAM MUST COMPILE CLEANLY WITH: gcc -o wc wc.c -Wall -pedantic
*/
/*
* This should return the number of spaces in the buffer using the isspace
* function.
*/
static size_t
words(const char* buffer, size_t length)
{
size_t sum = 0;
return sum;
}
/*
* This should return the number of lines in the buffer by checking for the
* existence of ' ' characters in the buffer.
*/
static size_t
lines(const char* buffer, size_t length)
{
size_t sum = 0;
return sum;
}
int
main(int argc, const char* argv[])
{
char buffer[256];
FILE* fp;
size_t bytes_read;
size_t sum = 0;
enum {
MODE_LINES,
MODE_WORDS
} mode;
bool middle = false;
if (argc != 3) {
printf("Usage: ./wc ");
exit(EXIT_FAILURE);
}
/*
* Set mode here based on the value of argv[1]. Use the
* strcmp function for string comparison.
*/
fp = fopen(argv[2], "r");
if (fp == NULL) {
printf("error: could not open %s: %s ",
argv[1], strerror(errno));
exit(EXIT_FAILURE);
}
for (;;) {
bytes_read = fread(buffer, 1, sizeof(buffer), fp);
if (bytes_read == 0)
break;
if (mode == MODE_LINES) {
sum += lines(buffer, bytes_read);
}
else if (mode == MODE_WORDS) {
sum += words(buffer, bytes_read);
}
}
if (argc != 3) {
printf("Usage: ./wc ");
exit(EXIT_FAILURE);
}
/*
* Set mode here based on the value of argv[1]. Use the
* strcmp function for string comparison.
*/
fp = fopen(argv[2], "r");
if (fp == NULL) {
printf("error: could not open %s: %s ",
argv[1], strerror(errno));
exit(EXIT_FAILURE);
}
for (;;) {
bytes_read = fread(buffer, 1, sizeof(buffer), fp);
if (bytes_read == 0)
break;
if (mode == MODE_LINES) {
sum += lines(buffer, bytes_read);
}
else if (mode == MODE_WORDS) {
sum += words(buffer, bytes_read);
}
}
/*
* You need to increment sum if you were in the middle of a line or word and end of
* file was hit.
*/
printf("%zu ", sum + middle);
return 0;
}
Please solve the Bolded Questions usong the C Language.
Thanks!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions