Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modifi the following program, using c++ with a linux base system. Using the requ

ID: 3873047 • Letter: M

Question

Modifi the following program, using c++ with a linux base system. Using the requirements stated.

This program is meant to give students practice working on projects that contain multiple files of source code. It will require to construct a Makefile and appropriate header files. It will also include the use of getopt to handle optional command line parameters. Manipulating data read in from a file will also be introduced.

The following functions will be of use to you (read and write are mandatory).

read(2)

write(2)

getopt(3)

Description

In this assignment, you will be creating an upgrade to the cat command you implemented in assignment 2. Let’s call it dog because canines are so obviously better than felines. The features that will be added to this program include:

The ability to specify a command line parameter (-b x ) to change the size of the buffer used with read and write to x.

The ability to specify a command line parameter (-n x ) to change the number of bytes read from each file to ex. (Instead of reading the entire file.)

The ability to specify a command line parameter (-c x ) to change the shift to x when applying a simple Caesar cipher to text within the file (described below).

The ability to specify a command line parameter (-r x ) to specify the shift to x when applying a similar rotation to binary data within the file.

The ability to specify a command line parameter (-x ) to output the data in the file as hexadecimal numbers.

Requirements

The main loop should be in your main function in one source code file. The functions that perform the encryption should be found in another file. Their declarations will need to be in a header file so that they can be called from main even though their implementations are in another file.

You must make a Makefile that has rules sufficient to compile your program when a user types make in the same directory as your source code. Only files that have changed or have had their dependencies change should be recompiled.

You must use the system calls read and write to do the input and output for the program.

Obviously, your source code must be well documented. If it is not, there will be a large pointpenalty. This requirement holds for all assignments, whether this line is in the writeup or not.

The features enabled by the command line parameters listed in the description section abovemust be implemented.

Note: If both -r and -c are specified, the behavior can change depending on which is executed first. Don’t allow a user to specify both: give them an error message if they try. The other command line parameters can all be on at the same time without any problem. Make sure that they work together.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
#define read_buffer_size 8192


int main(int argc, char* argv[])
{

   //file descriptor for opening and closing

   int file_descriptor; //size of buffer for file reading
   char read_buffer[read_buffer_size];
   ssize_t read_bytes;


   //looping through all files given in argument
   int i;
   for(i=1;i<argc;i++)
   {
           //using linux system call open file with readonly flag
       file_descriptor = open (argv [i], O_RDONLY);
           //if file not opened
       if (file_descriptor == -1)
       {
           perror ("open");
           printf("file can not be opened : %s ",argv[i]);
       }
  
           else
           {
               while((read_bytes = read (file_descriptor, &read_buffer, read_buffer_size)) > 0)
               {  
                   printf("%s",read_buffer);
               }
               //once done with file closig the file using file descriptor

               close (file_descriptor);
           }
     }

   return (EXIT_SUCCESS);
}

Explanation / Answer

Setting up to compile C++ on Linux/Unix

How you set up to compile C++ programs depends on whether you are using Windows or Linux/Unix. This page describes the approach to use if you have a Linux/Unix machine. If you have a Windows machine, read this page instead.

To establish that your Linux/Unix system has the correct C++ compiler installed, at the command prompt type this:

If your system is set up correctly, this command will launch the compiler executable and print its version. If an error message is printed instead, you will have to consult your documentation to make sure the compiler is installed and set up correctly.

When I compile on Linux and other versions of Unix, I use an executable shell file named gccp with this content:

If you want this shell file, simply copy it from this page and save it on your system in a file named gccp. Be sure to (1) make the file executable, and (2) place it in an accessible directory. Because I cannot know which version of Unix you are running, I can't really be more specific.

This shell file sets up the most strict ANSI standard compliance level, making it necessary for the programmer to pay attention to many compatibility and style issues. There may even be some incompatibilities within the C++ compiler's own library routines, so this shell file may not always result in a successful compile even if your program is flawless. But in general, it is a good idea to establish high standards as a student, to become accustomed to good programming style.

If you execute the compiler directly, without using the shell file, its behavior is more relaxed. This may sometimes be necessary. Do it like this:

Now set up a convenient data directory in which to place your programs. Create a file named temp.cpp with this content:

Again, you should be able to copy this little program from this page, and use a text editor to save it as “temp.cpp.”

Move to the directory in which temp.cpp is located, and type this:

If you have not made any errors, the program will compile uneventfully, and a program file named “a.out” will be created in the same directory.

To execute your program, type “./a.out” (your system may require slightly different syntax) and press Enter. The program should run, and print “Hello world!” on the display.

This series of actions confirms that you have acquired a C++ compiler and it is working properly.

Linux and other versions of Unix usually have many worthwhile programming editors available, many of which can be customized to suit your requirements. But, again, because I cannot know which Unix you are running, I can't be specific. Be sure to look into the many editors that are available — they save a great deal of effort in programming.

And learn how to create a variety of executable shell files — they save a great deal of time on Unix systems.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote