Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Topic Video
Question

In C++

Implement a simple version of the linux grep command in C++. grep - Looks through a file, line by line, trying to find a user-specified search term in the line. If a line has the word within it, the line is printed out, otherwise it is not. Use the system calls open(), getline(), close(). 

Requirements (examples run from. terminal)

  • Your program grep is always passed a search term and zero or more files to grep through (thus, more than one is possible). It should go through each line and see if the search term is in it; if so, the line should be printed, and if not, the line should be skipped.
    • [terminal]$ ./grep ! main.cpp main2.cpp

      cout << "Hello, World!";

      cout << "Programming is great fun!";
  • The matching is case sensitive. Thus, if searching for world, lines with World will not match. 
  • Lines can be arbitrarily long (that is, you may see many many characters before you encounter a newline character, \n). grep should work as expected even with very long lines. For this, you might want to look into the getline() library call.
  • If grep is passed no command-line arguments, it should print "wgrep: searchterm [file ...]" (followed by a newline) and exit with status 1.
    • [termianl]$ ./grep

      wgrep searchterm [file ...]
  • If grep encounters a file that it cannot open, it should print "grep: cannot open file" (followed by a newline) and exit with status 1.
    • [terminal]$ ./grep World main3.cpp

      wgrep: cannot open file
  • In all other cases, grep should exit with return code 0.
  • If a search term, but no file, is specified, grep should work, but instead of reading from a file, grep should read from standard input.
    • [terminal$ ./grep World

      Hello World

      Hello World

      Programming is great fun!

      Programming for real world problems is complex!

      Programming makes the World go round

      Programming makes the World go round

      ^C
  • For simplicity, if passed the empty string as a search string, grep can either match NO lines or match ALL lines, both are acceptable. Here is an example of returning everything.
    • [terminal]$ ./grep "" main.cpp

      #include <iostream>

      using namespace std;

      int main(){

      cout << "Hello, World!";

      return 0;

      }
  • If a search term consists of multiple words, the grep should work as follows:
    • [terminal]$ ./grep "Hello, World" main.cpp

      cout << "Hello, World!";
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education