Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Your program can count the total number of words in a given file. When the input has two arguments ./a.out text1.txt Output: The total number
Your program can count the total number of words in a given file.
When the input has two arguments ./a.out text1.txt
Output:
The total number of words in text1.txt is 42.
The total number of characters in text1.txt is 163.
#include #include #include #include #include using namespace std; #define PMODE 0644 int main(int argc, char *argv[]){ int fdin, n; char buf[BUFSIZ]; if (argc < 2) { fprintf(stderr, "Usage: %s file_name ",argv[0]); exit(1); } if((fdin = open(argv[1], O_RDONLY)) == -1){ perror(argv[1]); exit(2); } while((n = read(fdin, buf, BUFSIZ)) > 0){ cout << buf ; //printf("%s",buf); } close(fdin); exit(0); }
text1.txt
hello world! I like apple hello world! I like Apple hello world! I like Banana hello world! I like apples hello world! hello world! hello world! hello world! hello world! hello world! 1 2 3 4 5 6 7 8 9 10
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started