Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In terms of your task, it is fairly easy to describe. You will be writing a simple search and replace application. Specifically, your job is

In terms of your task, it is fairly easy to describe. You will be writing a simple search and replace application. Specifically, your job is to (1) find a user-specified text string within a group of disk files, (2) modify those strings and update the original disk files, and then (3) provide a report that indicates what has been done. It will work as follows:

1. The application will take a single command argument. This argument will be interpreted as a text string. For example, if your program was called replace.exe, you might enter replace.exe apple. Note that if you are using the Eclipse C/C++ plugin as your IDE, you can use a Run Configuration to specify the command line argument.

2. You will then open each of the files in the current directory. If you havent done much programming with files and directories, it isnt especially difficult. That said, the facilities for doing so in C are somewhat more primitive than those in languages such as Java. First, you will want to make use of the functions associated with . As we discussed in class, these are the basic IO functions, and can be applied not only to the screen and keyboard, but to disk files as well (There are countless online examples of basic C I/O). Here, you will use functions for opening and closing files, reading and writing bytes/chars to and from files, and for checking to see to see if you have reached the end of the file.

3. In addition to opening and reading files, you must also be able to read the contents of the directory in order to see what files need to be checked (the current directory = .). Basically, a directory is just a special type of file that contains a list of the files within it. So the idea is to just loop through the file list, and open and examine each text file (for simplicity, all files used during testing will be text files with a .txt suffix, so you dont have to worry about other formats). Unfortunately (for you), reading directories is operating system dependent since it is closely associated with the file system used by the host operating system.

For each text file found in the directory, you will open each and check for the string that was entered at the command line. The target string may occur multiple times, of course. When you find it, you will modify the text so that the target string is printed in upper case. So, for example, if apple is the target, the string the Big Apple is fun would become the Big APPLE is fun. Please note that while this kind of update can be done with complex regular expression libraries, you will not do that here. Instead, you will just be flipping the lowercase letters to uppercase as required. String related functions are found in while character functions are in .

To make things just a little more interesting, directories can be nested. So if the current directory has sub directories, you must search those as well. Note that when you read the directory contents in the main WHILE loop, you can check each entry to determine if it is a regular file or another directory (there are constants defined in for this purpose.). Again, there are simple examples of this online.

Finally, you must keep track of the files that were changed and the number of changes made in each one. At the end of the process (when no more directories can be found from your starting point), you must print out a report. The report will simply list each changed file, along with the count of the changes in each. The files should be ordered/sorted on the number of changes, with the most heavily modified listed first. Note that you do not have to write your own sort algorithm. Instead, provides a sorting function called qsort, that can be used to sort arbitrary items. To use qsort, you must simply provide a comparison function that qsort can use (Java uses a similar logic for sorting objects)

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 Administration The Complete Guide To Dba Practices And Procedures

Authors: Craig S. Mullins

2nd Edition

0321822943, 978-0321822949

More Books

Students also viewed these Databases questions