Write a program, that reads a file named quote.dat and encrypts that file, (usin
ID: 3548251 • Letter: W
Question
Write a program, that reads a file named quote.dat and encrypts that file, (using a basic encryption) and stores it in a file named encryp_quote.dat. Then your program must read encryp_quote.dat, decrypt it and store it in a file named decryp_quote.dat. After the program is ran, you should be able to examine all three *.dat files. You must use a quote of Albert Einstein and Yogi Berra in your quote.dat file and of course, the quote you use must begin with a capital letter and end with a period, question mark or exclamation point.
Quotes to be used:
Yogi Berra quote "You've got to be very careful if you don't know where you are going because you might not get there."
Albert Einstien qoute
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/* For exit() function*/
int main()
{
char c[1000],a[1000];
FILE *fptr,*fp,*f,*fpp;
if ((fptr=fopen("quote.dat","r"))==NULL)
{
printf("Error! opening file");
exit(1);
}
fscanf(fptr,"%[^ ]",c);
// file in string c
fclose(fptr);
int n;
n=strlen(c);
int i;
for(i=0;i<n;i++)
c[i]=c[i]+3;
//file encrypted
fp = fopen("encryp_quote.dat", "w+");
fputs(c, fp);
fclose(fp);
//encrypted file written to file
if ((f=fopen("encryp_quote.dat","r"))==NULL)
{
printf("Error! opening file");
exit(1);
}
fscanf(f,"%[^ ]",a);
fclose(f);
for(i=0;i<n;i++)
a[i]=a[i]-3;
// decrypting and written back to file
fpp = fopen("decrypt_quote.dat", "w+");
fputs(a, fpp);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.