Skip to content

Implemented a program that reads command line arguments and performs find/replace on files.

License

Notifications You must be signed in to change notification settings

zhen69/Find-and-Replace-Text-in-File

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Find-and-Replace-Text-in-File

Implemented a program that reads command line arguments and performs find/replace on files.

Description

This program would perform the find and replace function on the specified input file. Unless the same file is entered as both the input and output files, the input file will never be modified. The results of the find/replace would be printed in the specified output file (Note: output file would be overwritten if the file is not empty). To use this program, users must enter a command line using the required flags (see below) to indicate the search replacement texts. The command should also state the input file and output file. Despite that, there are optional flags that individuals can use to modify the replacement process. For example, users can use the l flag to indicate which lines the find/replace function should be performed and the w flag to perform prefix/suffix replacement (replace all words with the specified prefix/suffix).

Flags:

  • -s [string: search text] (required) (ex. -s world)
  • -r [string: replacement test] (required) (ex. -r Hello)
  • -l [int: start, int: end] (optional) (ex. -l 1,2 => only perform the find/replace function between lines 1 and 2 inclusive)
  • -w (optional) (no argument, but search text must be entered as *world for suffix find/replace or world* for prefix find/replace)

Note/Warning:

  • The input order doesn't matter for the flags listed above.
  • The input and output files must be entered as the last two command line arguments, with input coming before output
  • Due to certain reasons, the prefix/suffix find/replace function doesn't support any words that start with a space or punctuation

Error Codes - When the input command line is invalid, the program returns an error code in the following order of precedence:

  1. MISSING_ARGUMENT (code 1 - when the input command line is less than seven arguments)
  2. DUPLICATE_ARGUMENT (code 8 - when a flag is entered twice)
  3. INPUT_FILE_MISSING (code 2 - when the indicated input file couldn't be opened or doesn't exist)
  4. OUTPUT_FILE_UNWRITABLE (code 3 - when the indicated output file is not writable)
  5. S_ARGUMENT_MISSING (code 4 - when the s flag or s flag argument is missing)
  6. R_ARGUMENT_MISSING (code 5 - when the r flag or r flag argument is missing)
  7. L_ARGUMENT_INVALID (code 6 - when the l flag argument is invalid)
  8. WILDCARD_INVALID (code 7 - when the w flag is entered but the search text indicated by the s flag is invalid)

Get Started

Dependencies

Installing

Reference: https://docs.github.com/en/get-started/quickstart/fork-a-repo
  1. Fork this repository by clicking the Fork button located on the top of the page
  2. Navigate to "Your Repositories" on GitHub and open the forked repo
  3. In the terminal, git clone the repo using the URL or SSH key

Executing Program

Run with makeFile:

In the terminal, type make and make test to run the test cases written in unit_tests.c. Individuals may use the existing code as an example and write their test cases.

Note: If including your own test cases, you must include your input file in tests.in.orig and expected output file in tests.out.exp.

Run with GCC:

In the terminal, type gcc [source file]. By default, this will produce an executable file named a.out (Linux) or a.exe (Windows). If you want the executable file to have a custom name, type gcc -o [name of executable] [source file]. After compiling the program, run the executable file by entering ./[executable file] [command line arguments for running the program].

Author

Zhen Wei Liao - @Zhen
Built the main program and parts of FindReplace.h/unit_tests.c

License

This project is under the MIT License.
Please check the LICENSE.md file for more information.