Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please show the code for both input and output and also show how to clear the comments to transfer back to new file. thanks Program

please show the code for both input and output and also show how to clear the comments to transfer back to new file. thanks image text in transcribed
image text in transcribed
image text in transcribed
Program Description: This program will be written in the Clanguage, and will remove comments from C programs. You may not use any libraries other than the library, nor call any external programs. In other words, all the work must occur in your program Comments in C programs may appear in two different formats: /* text contained in these slash-star/star-slash pairs inclusive */ // Double-slashes and any text that follows until the end of line Your program will open a file (as named on the command-line), read from this file and write to another file which you create. You will be making a copy of the named file. But in the process you will remove any legal C comments that you find. All other text must be copied to the new file without modification. To obtain the input filename, you will need to access the command line. The command line is stored as an array of strings. Each element in the array is a null-terminated array of characters. The array itself is terminated with a NULL pointer, although you can learn the length of the array from the array length which is also passed in There are two arguments passed into main which are of interest to us. The first is the argument count (argc) which tells us how many non-null elements are in the argument array (or argument vector). The second argument is the array of command-line arguments (argv) which is an array of character pointers (strings) where each non-null pointer contains the address in memory of a string containing one command line argument. This argu array is itself terminated by a NULL pointer (a pointer containing address 0x00000000, which is not a legal address on the system). You can determine the number of command line arguments either from argc, or by iterating over the argv array until that NULL pointer is seen. Main may therefor be declared as: int main(int arge, char **argv) The following diagram shows the argv array corresponding to the following command-line: Ja.out one two three Note how each element in argy points to contains the address of) the corresponding argument. The first element in the array (argv[0]) points to a string with contents"./a.out". This string is terminated with a null byte (different from a NULL pointer). A null byte is a byte with value 0, also shown as the escaped character '\0'. The next element (argv[1]) points to "one", then argv[2] points to "two", and finally argv[31 points to the string "three". The last element in argv (argv[4] is NULL, and does not point to anything. There are no arguments after three". Thus there are 4 non null pointers in the argv array (argv[0) through argv[3]) and the argc counter is therefore equal to 4 argy NULL leno # 'w'd 110 'd in leng 10 The first argument (argv[0]) is always the name of the program being ran, and is of no interest to us right now. The second argument (argv[1]), if present would be the word immediately following the program name. Command-line arguments generally do not contain spaces, so you can treat them as individual words at this point. Be particularly careful not to assume that argv(1) exists, as there is no guarantee that the user typed this value. You must verify that this argument exists by either making sure that argc is at least 2, or that the first two elements in argv are non-NULL. File I/O was demonstrated in class. Once you have the name of an input file, use open (in section 2) to open the file (as well as another file for output), use read (also in section 2) to read chunks of the file, and use write (also in section 2) to write You can ask the preprocessor to perform a similar test for you. This is not a valid solution for the assignment, but might be a useful test if you are unclear about what it means to remove comments: S cpp-fpreprocessed dD-E test.c> test.new preprocessed indicates that the file has already been preprocessed (disables much of cpp) The-dD switch disables #define directives The -P switch disables line markers . Example Output Here is a sample input file: #include // This debug flag doesn't really do anything yet #define DEBUG 1 int main() /* This is an example of testing the debug flag"/ Nif DEBUG printf("About to print hello "); tiendif printf("hello "); /* end of main / The corresponding output #include #define DEBUG 1 int main() Hif DEBUG printf ("About to print hello "); Hendil printf("hello "); Assignment Objective: Copy the contents of a file (named on the command line) to another file, while removing any legal comments Requirements: Your solution must be written using the language. It must compile and run on the CSE machines You should include your name (under which you registered) in a comment at the top This is an individual programming assignment that must be the sole work of the individual student. Any instance of academic dishonesty will result in a grade of "F" for the course, along with a report filed with the Academic Integrity Database Program Description: This program will be written in the Clanguage, and will remove comments from C programs. You may not use any libraries other than the library, nor call any external programs. In other words, all the work must occur in your program Comments in C programs may appear in two different formats: /* text contained in these slash-star/star-slash pairs inclusive */ // Double-slashes and any text that follows until the end of line Your program will open a file (as named on the command-line), read from this file and write to another file which you create. You will be making a copy of the named file. But in the process you will remove any legal C comments that you find. All other text must be copied to the new file without modification. To obtain the input filename, you will need to access the command line. The command line is stored as an array of strings. Each element in the array is a null-terminated array of characters. The array itself is terminated with a NULL pointer, although you can learn the length of the array from the array length which is also passed in There are two arguments passed into main which are of interest to us. The first is the argument count (argc) which tells us how many non-null elements are in the argument array (or argument vector). The second argument is the array of command-line arguments (argv) which is an array of character pointers (strings) where each non-null pointer contains the address in memory of a string containing one command line argument. This argu array is itself terminated by a NULL pointer (a pointer containing address 0x00000000, which is not a legal address on the system). You can determine the number of command line arguments either from argc, or by iterating over the argv array until that NULL pointer is seen. Main may therefor be declared as: int main(int arge, char **argv) The following diagram shows the argv array corresponding to the following command-line: Ja.out one two three Note how each element in argy points to contains the address of) the corresponding argument. The first element in the array (argv[0]) points to a string with contents"./a.out". This string is terminated with a null byte (different from a NULL pointer). A null byte is a byte with value 0, also shown as the escaped character '\0'. The next element (argv[1]) points to "one", then argv[2] points to "two", and finally argv[31 points to the string "three". The last element in argv (argv[4] is NULL, and does not point to anything. There are no arguments after three". Thus there are 4 non null pointers in the argv array (argv[0) through argv[3]) and the argc counter is therefore equal to 4 argy NULL leno # 'w'd 110 'd in leng 10 The first argument (argv[0]) is always the name of the program being ran, and is of no interest to us right now. The second argument (argv[1]), if present would be the word immediately following the program name. Command-line arguments generally do not contain spaces, so you can treat them as individual words at this point. Be particularly careful not to assume that argv(1) exists, as there is no guarantee that the user typed this value. You must verify that this argument exists by either making sure that argc is at least 2, or that the first two elements in argv are non-NULL. File I/O was demonstrated in class. Once you have the name of an input file, use open (in section 2) to open the file (as well as another file for output), use read (also in section 2) to read chunks of the file, and use write (also in section 2) to write You can ask the preprocessor to perform a similar test for you. This is not a valid solution for the assignment, but might be a useful test if you are unclear about what it means to remove comments: S cpp-fpreprocessed dD-E test.c> test.new preprocessed indicates that the file has already been preprocessed (disables much of cpp) The-dD switch disables #define directives The -P switch disables line markers . Example Output Here is a sample input file: #include // This debug flag doesn't really do anything yet #define DEBUG 1 int main() /* This is an example of testing the debug flag"/ Nif DEBUG printf("About to print hello "); tiendif printf("hello "); /* end of main / The corresponding output #include #define DEBUG 1 int main() Hif DEBUG printf ("About to print hello "); Hendil printf("hello "); Assignment Objective: Copy the contents of a file (named on the command line) to another file, while removing any legal comments Requirements: Your solution must be written using the language. It must compile and run on the CSE machines You should include your name (under which you registered) in a comment at the top This is an individual programming assignment that must be the sole work of the individual student. Any instance of academic dishonesty will result in a grade of "F" for the course, along with a report filed with the Academic Integrity Database

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

Advances In Databases And Information Systems Second East European Symposium Adbis 98 Poznan Poland September 1998 Proceedings Lncs 1475

Authors: Witold Litwin ,Tadeusz Morzy ,Gottfried Vossen

1st Edition

3540649247, 978-3540649243

More Books

Students also viewed these Databases questions

Question

2. Identify conflict triggers in yourself and others

Answered: 1 week ago