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
  • 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...

  • 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...

  • 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()...

  • MASM Assembly language -- Message Encryption Pgm You are to write a program to input a...

    MASM Assembly language -- Message Encryption Pgm You are to write a program to input a text and a key and encrypt the text. I will supply you an encryption key consisting of multiple characters. Use this key to encrypt and decrypt the plain text by XOR-ing each character of the key against a corresponding byte in the message. Repeat the key as many times as necessary until all plain text bytes are translated. Suppose, for example, the key were...

  • 1. Specification Write a C program to implement a simple calculator that accepts input in the...

    1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...

  • Hi, need this question ansered in c++, has multiple levels will post again if you can...

    Hi, need this question ansered in c++, has multiple levels will post again if you can complete every level so keep an eye out for that. here is a sketch of the program from the screenshot int main (int argc, char** argv) { enum { total, unique } mode = total; for (int c; (c = getopt(argc, argv, "tu")) != -1;) { switch(c) { case 't': mode = total; break; case 'u': mode = unique; break; } } argc -=...

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
ADVERTISEMENT