Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task Description Implement a program sub that makes a copy of the content of a file substituting one character for another. The program should print

Task Description

  1. Implement a program sub that makes a copy of the content of a file substituting one character for another.

The program should print the following help message that explains the functionality of the utility:

USAGE: sub [ -h | --fromChars -+toChars [-i inputFile] [-o outputFile] ] DESCRIPTION: This utility copies text from an input stream to an output stream replacing every instance of a specific character in fromChars with a corresponding (position-wise) character from toChars. Any characters in fromChars and in toChars that do not have corresponding counterparts in the other one are ignored. If an input file is provided, the content is read from that file; otherwise, the standard input is used. If an output file is provided, then the modified content is written to that file; otherwise, the standard output is used. OPTIONS: --(followed by a string without separating space) indicates all characters that will be replaced in the processed text -+(followed by a string without separating space) indicates the characters that will be used to replace corresponding (position-wise) characters from fromChars in the processed text -i (followed by input file name) 

Submission

You must submit the following:

  • the signed source code,
  • the CMakeLists.txt for building the program.
  • test scripts that show robust testing sessions applying all described usage combinations.
 use the provided file as an input stream instead of standard input -o (followed by output file name) use the provided file as an output stream instead of standard output -h prints this help message; it is also a default if no command line arguments are provided 

Examples

  • $ sub $ sub -h

    Both invocation should display the help information for the utility as specified earlier.

  • $ sub --a -+x -i src.txt -o dest.txt $ sub -i src.txt -o dest.txt --a -+x $ sub -o dest.txt --a -i src.txt -+x $ sub --a -o dest.txt -+x -i src.txt 

    All commands with any order of the arguments copy the content of src.txt to dest.txt with all instances of "a" in src.txt replaced by "x" in dest.txt. NOTE: character case is enforced; i.e., 'a' is different from 'A', and'x' is different from 'X'.

  • $ sub --a -+x -i src.txt 

    This will copy the content of src.txt to the standard output with all instances of "a" replaced with "x".

  • $ sub --a -+x -o dest.txt 

    This will copy user input from standard input to dest.txt with all instances of "a" replaced with "x".

  • $ sub --a -+x 

    This will copy user input from standard input to standard output with all instances of "a" replaced with "x".

  • $ sub --ab -+xyz 

    In this case all instances of "a" should be replaced by "x" and all instances of "b" should be replaced with "y". The letter "z" will be ignored and the following warning message will be output:

    sub: WARNING - extraneous replacement character 
  • $ sub --abc -+xy 

    This is an error, so no replacements will take place. Instead, the following error message will be output:

    sub: ERROR - missing replacement character 

Implementation

Put all code into a file sub.c (and accompanying sub.h if necessary). Apply the convenience file opening function freopen() to open input and output streams. Check its usage with:

$ man freopen

Other functions that you should use are described in their respective man pages:

$ man getc $ man putc

Create CMakeLists.txt and use cmake and make to create an executable sub as explained in the previous lab.

Important Notes:

Your must:

  • use freopen() to switch the input/output streams in the case of -i and -o flags,
  • process the program arguments sequentially using a loop; DO NOT hardcode argument positions,
  • follow the examples to test your program on several different input files with substantial heterogeneous content and a variety of letters being replaced, and
  • submit the lab solution applying the submission standards specified in the syllabus.

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

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago