Question

The question asks you to assume that your input file is an English text that may...

The question asks you to assume that your input file is an English text that may contain any character that appears on keyboard of a computer. The task is to read each character from the input file and after encrypting it by adding an integer value n (0 < n < 128) to the character, to write the encrypted character to output file.

We are supposed to use command line arguments and utilize argc, and argv parameters in main function of the code.

For example:

Command line arguments

       assign2 –e n infile outfile  

  or

      assign2 –d infile outfile  

The -e indicates that you would like to encrypt infile and write the result to outfile, while n indicates the number (0 < n < 128). The -d indicates that you want to decrypt infile (of course the file should have been encrypted) and write the result to outfile using the data from the infile.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the code which does the job:

#include <stdio.h>
#include <stdlib.h>

void encrypt(int n, char fname[], char op_file[]){
   char ch; FILE *fpts, *fptt;
   fpts=fopen(fname, "r");
   if(fpts==NULL)
   {
       printf(" File does not exists or error in opening..!!");
       exit(1);
   }
   fptt=fopen(op_file, "w");
   if(fptt==NULL)
   {
       printf(" Error in creation of file temp.txt ..!!");
       fclose(fpts);
       exit(2);
   }
   while(1)
   {
       ch=fgetc(fpts);
       if(ch==EOF)
       {
           break;
       }
       else
       {
           ch=ch+n;
           fputc(ch, fptt);
       }
   }
   fclose(fpts);
   fclose(fptt);
   fpts=fopen(fname, "w");
   if(fpts==NULL)
   {
       printf(" File does not exists or error in opening..!!");
       exit(3);
   }
   fptt=fopen(op_file, "r");
   if(fptt==NULL)
   {
       printf(" File does not exists or error in opening..!!");
       fclose(fpts);
       exit(4);
   }
   while(1)
   {
       ch=fgetc(fptt);
       if(ch==EOF)
       {
           break;
       }
       else
       {
           fputc(ch, fpts);
       }
   }
   printf(" File %s successfully encrypted ..!!\n\n", fname);
   fclose(fpts);
   fclose(fptt);
}

void decrypt(char fname[], char op_file[]){
   char ch;
   FILE *fpts, *fptt;
  
   fpts=fopen(fname, "w");
   if(fpts==NULL)
   {
       printf(" File does not exists or error in opening..!!");
       exit(7);
   }
   fptt=fopen(op_file, "r");
   if(fptt==NULL)
   {
       printf(" File does not exists or error in opening..!!");
       fclose(fpts);
       exit(9);
   }
   while(1)
   {
       ch=fgetc(fptt);
       if(ch==EOF)
       {
           break;
       }
       else
       {
           ch=ch-100;
           fputc(ch, fpts);
       }
   }
   printf(" The file %s decrypted successfully..!!\n\n",fname);
   fclose(fpts);
   fclose(fptt);

}

void main(int argc, char *argv[])
{
   if( argc ==5){
       encrypt(atoi(argv[2]),argv[3],argv[4]);
   }
   else if(argc==4){
       decrypt(argv[2],argv[3]);
   }
   else if(argc > 5){
       printf("too many arguments provided\n");
   }
   else if(argc < 4){
       printf("few arguments provided\n");
   }
}

Add a comment
Know the answer?
Add Answer to:
The question asks you to assume that your input file is an English text that may...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

    Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested for file open.” Append to the program to output to the text log file a new line starting with day time date followed by the message "SUCCESSFUL". Append that message to a file “7Error_Log_File.txt” . ?newline Remember to be using fprintf using stderr using return using exit statements. Test for file existence, test 7NoInputFileResponse.txt file not null (if null...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • I am writing a program in C++, which requires me to read an input text file...

    I am writing a program in C++, which requires me to read an input text file using command line argument. However, I am using xcode on my Macbook to write C++ program, and use terminal instead of command. How do you use int main(int argc, char** argv[]) to read an input file. My professor requires us not to hard code the text file name like .open("example.txt"); Thank you!

  • Write a complete C program that inputs a paragraph of text and prints out each unique...

    Write a complete C program that inputs a paragraph of text and prints out each unique letter found in the text along with the number of times it occurred. A sample run of the program is given below. You should input your text from a data file specified on the command line. Your output should be formatted and presented exactly like the sample run (i.e. alphabetized with the exact spacings and output labels). The name of your data file along...

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

  • Background: For this assignment, you will write a small encryption utility that implements a simple encryption...

    Background: For this assignment, you will write a small encryption utility that implements a simple encryption algorithm described below. The program will take one command line argument as an input; this will represent the word which is to be encrypted. As an output, your program will print the encrypted version of the word to the console using a simple printf() statement. This is the only output your program needs to produce. There is an important catch, however: your program is...

  • 1. Suppose you wrote a program that reads data from cin. You are now required to...

    1. Suppose you wrote a program that reads data from cin. You are now required to reimplement it so that you can read data from a file. You are considering the following changes. I. Declare an ifstream variable in_file II. Replace all occurrences of cin with in_file III. Replace all occurrences of > > and get_line with the appropriate operations for ifstream objects What changes do you need to make? I, II, and III II and III I and III...

  • I need help programming this question using C language. This program uses input data entered in...

    I need help programming this question using C language. This program uses input data entered in command line at the same time when the command or .exe file is entered. They are called command-line arguments. Because everything entered in command line is taken as a string, these arguments including the command itself or the .exe file name are stored in an array of char pointers, each pointer points to the first character of a string (i.e., argument). The inputs can...

  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT