Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Test for the code Reverse - Works correctly with no test file (kbd to screen) (1 pts) Reverse - Works correctly with 1 test argument

image text in transcribed

image text in transcribed

image text in transcribed

Test for the code

Reverse - Works correctly with no test file (kbd to screen) (1 pts)

Reverse - Works correctly with 1 test argument file (1.5 pts)

Reverse - Works correctly with 2 test argument files (2 pt)

Please include comments

Reverse Introduction This project is another warm-up to get you used to how this whole project thing will go. It also serves to get you into the mindset of a C programmer, something you will become quite familiar with over the next few months. Good luck! You will write a simple program called reverse. This program should be invoked in one of the following ways: prompt> /reverse prompt> ./reverse input.txt prompt> ./reverse input.txt output.txt The above line means the users typed in the name of the reversing program reverse (the ./ in front of it simply refers to the current working directory (called dot, referred to as.) and the slash (/) is a separator; thus, in this directory, look for a program named reverse) and gave it either no command-line arguments, one command-line argument (an input file, input.txt), or two command-line arguments (an input file and an output file output.txt). An input file (one to be reversed) comprised of 4 lines might look like this: hello this is a file The goal of the reversing program is to read in the data (lines) from the specified input file and reverse it; thus, the lines should be printed out in the reverse order of the input stream. Thus, for the aforementioned example, the output should be: a file is this hello There are different ways to invoke the reverse program (as above shown), and all correspond to slightly different ways of using this simple new Unix utility. For example, when invoked with two command-line arguments, the program should read from the input file (specified in the first argument) the user supplies and write the reversed version of said file to the output file (specified in the second argument) the user supplies. The goal of the reversing program is to read in the data (lines) from the specified input file and reverse it; thus, the lines should be printed out in the reverse order of the input stream. Thus, for the aforementioned example, the output should be: a file is this hello There are different ways to invoke the reverse program (as above shown), and all correspond to slightly different ways of using this simple new Unix utility. For example, when invoked with two command-line arguments, the program should read from the input file (specified in the first argument) the user supplies and write the reversed version of said file to the output file (specified in the second argument) the user supplies. When invoked with just one command-line argument, the user supplies the input file, but the file should be printed to the screen. In Unix-based systems, printing to the screen is the same as writing to a special file known as standard output, or stdout for short. Finally, when invoked without any arguments, your reversing program should read from standard input (stdin), which is the input that a user types in, and write to standard output (i.e., the screen). Sounds easy, right? It should. But there are a few details... Details Assumptions and Errors Input is the same as output:If the input file and output file are the same file, you should print out an error message "Input and output file must differ" and exit with return code 1 String length: You may not assume anything about how long a line should be. Thus, you may have to read in a very long input line... File length: You may not assume anything about the length of the file, i.e., it may be VERY Invalid files:If the user specifies an input file or output file, and for some reason, when you try to open said file (e.g., txt) and fail, you should print out the following exact error message: error: cannot open file 'input.txt' and then exit with return code 1 (i.e., call exit(1):). Malloc fails:If you call malloc() to allocate some memory, and malloc fails, you should print the error message malloc failed and exit with return code 1. Too many arguments passed to program:If the user runs reverse with too many arguments, print usage: reverse and exit with return code 1. How to print error messages:On any error, you should print the error to the screen using fprintf(), and send the error message to stderr (standard error) and not stdout (standard output). This is accomplished in your code as follows: fprintf(stderr, "whatever the error message is "); Useful Routines To exit, call exit(1). The number you pass to exit(), in this case 1, is then available to the user to see if the program returned an error (i.e., return a non-zero) or exited cleanly (i.e., returned o). For reading in the input file, the following routines will make your life easy: fopen(), getline(), and fclose(). For printing (to screen, or to a file), use fprintf(). Note that it is easy to write to standard output by passing stdout to fprintf(); it is also easy to write to a file by passing in the FILE * returned by fopen, e.g., fp=fopen(...); fprintf(fp, ...);. The routine malloc() is useful for memory allocation. Perhaps for adding elements to a list? If you don't know how to use these functions, use the man pages. For example, typing man malloc at the command line will give you a lot of information on malloc. Reverse Introduction This project is another warm-up to get you used to how this whole project thing will go. It also serves to get you into the mindset of a C programmer, something you will become quite familiar with over the next few months. Good luck! You will write a simple program called reverse. This program should be invoked in one of the following ways: prompt> /reverse prompt> ./reverse input.txt prompt> ./reverse input.txt output.txt The above line means the users typed in the name of the reversing program reverse (the ./ in front of it simply refers to the current working directory (called dot, referred to as.) and the slash (/) is a separator; thus, in this directory, look for a program named reverse) and gave it either no command-line arguments, one command-line argument (an input file, input.txt), or two command-line arguments (an input file and an output file output.txt). An input file (one to be reversed) comprised of 4 lines might look like this: hello this is a file The goal of the reversing program is to read in the data (lines) from the specified input file and reverse it; thus, the lines should be printed out in the reverse order of the input stream. Thus, for the aforementioned example, the output should be: a file is this hello There are different ways to invoke the reverse program (as above shown), and all correspond to slightly different ways of using this simple new Unix utility. For example, when invoked with two command-line arguments, the program should read from the input file (specified in the first argument) the user supplies and write the reversed version of said file to the output file (specified in the second argument) the user supplies. The goal of the reversing program is to read in the data (lines) from the specified input file and reverse it; thus, the lines should be printed out in the reverse order of the input stream. Thus, for the aforementioned example, the output should be: a file is this hello There are different ways to invoke the reverse program (as above shown), and all correspond to slightly different ways of using this simple new Unix utility. For example, when invoked with two command-line arguments, the program should read from the input file (specified in the first argument) the user supplies and write the reversed version of said file to the output file (specified in the second argument) the user supplies. When invoked with just one command-line argument, the user supplies the input file, but the file should be printed to the screen. In Unix-based systems, printing to the screen is the same as writing to a special file known as standard output, or stdout for short. Finally, when invoked without any arguments, your reversing program should read from standard input (stdin), which is the input that a user types in, and write to standard output (i.e., the screen). Sounds easy, right? It should. But there are a few details... Details Assumptions and Errors Input is the same as output:If the input file and output file are the same file, you should print out an error message "Input and output file must differ" and exit with return code 1 String length: You may not assume anything about how long a line should be. Thus, you may have to read in a very long input line... File length: You may not assume anything about the length of the file, i.e., it may be VERY Invalid files:If the user specifies an input file or output file, and for some reason, when you try to open said file (e.g., txt) and fail, you should print out the following exact error message: error: cannot open file 'input.txt' and then exit with return code 1 (i.e., call exit(1):). Malloc fails:If you call malloc() to allocate some memory, and malloc fails, you should print the error message malloc failed and exit with return code 1. Too many arguments passed to program:If the user runs reverse with too many arguments, print usage: reverse and exit with return code 1. How to print error messages:On any error, you should print the error to the screen using fprintf(), and send the error message to stderr (standard error) and not stdout (standard output). This is accomplished in your code as follows: fprintf(stderr, "whatever the error message is "); Useful Routines To exit, call exit(1). The number you pass to exit(), in this case 1, is then available to the user to see if the program returned an error (i.e., return a non-zero) or exited cleanly (i.e., returned o). For reading in the input file, the following routines will make your life easy: fopen(), getline(), and fclose(). For printing (to screen, or to a file), use fprintf(). Note that it is easy to write to standard output by passing stdout to fprintf(); it is also easy to write to a file by passing in the FILE * returned by fopen, e.g., fp=fopen(...); fprintf(fp, ...);. The routine malloc() is useful for memory allocation. Perhaps for adding elements to a list? If you don't know how to use these functions, use the man pages. For example, typing man malloc at the command line will give you a lot of information on malloc

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

Optimizing Data Collection In Warzones

Authors: Aaget Aamber

1st Edition

B0CQRRFP5F, 979-8869065902

More Books

Students also viewed these Databases questions

Question

What is IUPAC system? Name organic compounds using IUPAC system.

Answered: 1 week ago

Question

What happens when carbonate and hydrogen react with carbonate?

Answered: 1 week ago