Question
C Programming Question: 1. When we learned how to use the UNIX utility uniq in Lab 2, we learned that it can be used to
C Programming Question: 1. When we learned how to use the UNIX utility uniq in Lab 2, we learned that it can be used to display a file with all of its identical adjacent lines replaced by a single occurrence of the repeated line. In this question, we write a program that can perform a similar task, and the main difference is that it can detect identical lines that are not necessarily adjacent. Thus, this question asks you to write a C program that reads from stdin, filters matching lines from the input, and writes the distinct lines to the standard output. More precisely, when multiple lines in the input match, only the first of these lines will be printed to the output. This program also handles command-line arguments that specify rules that determine whether two lines match. For example, we have the following text file fruits.txt:
navel orange yellow banana red apple yellow banana red apple green apple navel orange green apple green mango navel orange strawberry red apple Red apple apple
We then run the following command: ./unique < fruits.txt
Without specifying any command-line options, we expect the program to print the following to stdout:
navel orange yellow banana red apple red apple green apple navel orange green mango strawberry Red apple apple
Either sub-question of this question will specify the exact behavior of this program precisely. The purpose of dividing this question into two sub-questions is to facilitate the awarding of partial marks. You are expected to submit one and only one set of source files and header files for the entire question. Thus the implementation and general requirements will be given before the sub-questions which will give the syntax of running the program.
Implementation Requirements: Read the input and store its distinct lines (the command-line arguments may be given to specify how to determine whether one line is distinct) in a linked list. Each distinct line is stored in one node of this linked list as a string variable. Each line of the input is expected to have at most 80 characters, excluding the terminating newline character. When one line of the input is too long, print an error message and terminate, without printing any line to stdout. This means that the string from each line can be stored in a character array of length 81.
You are expected to perform the task specified by each sub-question using this linked list. If your solution is not based-on such a linked list, you can get at most 30% of the total marks for this question.
General Requirements: Divide your source code into the following five files: unique.c: the file that contains the main function; lines.c and lines.h: the code for reading the lines from stdin and writing the lines to stdout (filtering matching lines can be done when you read the lines); match.c and match.h: the code for functions that determine whether two lines match, given options entered in the command-line.
In each module, divide your code into functions appropriately. In the header file, place prototypes of only those functions that are shared by difference modules. Prototypes of helper functions that are used in one module only should be placed in the corresponding .c file. Write a makefile that will allow us to use the following command on bluenose to compile your program to generate an executable file named unique:
make unique
(i) First, implement this unique program without considering command-line arguments. In this case, two lines match each other if and only if they contain identical strings. To run the program from a UNIX terminal using input redirection, we will enter the command
./unique < input_file
In this command line, input_file is the name of a plain text file. Each line is terminated by a trailing newline character, including the last line. You can assume that there are no empty lines in the input.
Error Handling: Your program should print an appropriate error message and terminate (without printing any lines from the input file) if there is at least one line that has more than 80 characters.
Testing: To help you confirm your understanding and make sure that the output format of your program is exactly what this question asks for, several files are provided in the following folder on bluenose to show how exactly your program will be automatically tested:
/users/faculty/prof2132/public/a7test/
In this folder, open the file a7q1atest to see the commands used to test the program with one test case. The output files, generated using output redirection, is also given. To check whether the output of your program on any test case is correct, redirect your output into a file, and use the UNIX utility diff (learned in Lab 3) to compare your output file with the output file in the above folder, to see whether they are identical. Construct additional input files to fully test your program, as we will use different cases when testing your program. Since these files are given, we will apply a penalty to any program that does not strictly meet the requirement on output format. Note that it is extremely important to use diff here, as the number of characters in the output is large. Hints: You can modify the read_line function given in class for your program, so that when the end of file is reached, an empty string will be read. Recall that the getchar function returns a macro EOF when end of file is reached. Thus the following logical expression might be helpful:
(ch = getchar()) != && ch != EOF
(ii) Next implement one more feature for this program by adding an option that requires the program to skip the first f fields of any line when determining whether two lines match. If a certain line has fewer than f fields, treat this line as an empty string, i.e. a string that only stores the null character, when matching it against other strings. For this we assume that each line of this file contains one or more fields separated by one or more space characters. To simplify your work, you can assume that other than the space characters used to separate the fields (there are no space characters after the last field of each line), and the trailing newline character, there are no other while-space characters.Thus, to run the program from a UNIX terminal using input redirection, we will enter the command
./unique f < input_file
In this command, f is a single digit between (and including) 0 and 9. Using the previous example, if we run
./unique 1 < fruits.txt
The output will be:
navel orange yellow banana red apple green mango strawberry
Note that the last line in the input file, i.e. apple, is not in the output. This is not because it matches any line that ends with apple, but because it matches strawberry! Find out why before starting the implementation.
Error Handling: In addition to the error case that you are supposed to handle in (i), your program should also print an appropriate error message and terminate (without printing any lines from the input file) if: The user supplies illegal command-line arguments; The number of command-line arguments supplied is incorrect (important: make sure that your program will still run without any command-line arguments as specified in (i)). If there is more than one problem with user input, your program just has to detect one of them. You can assume that everything else regarding the input file is correct.
Error Handling: In addition to the error case that you are supposed to handle in (i), your program should also print an appropriate error message and terminate (without printing any lines from the input file) if: The user supplies illegal command-line arguments; The number of command-line arguments supplied is incorrect (important: make sure that your program will still run without any command-line arguments as specified in (i)). If there is more than one problem with user input, your program just has to detect one of them. You can assume that everything else regarding the input file is correct.
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