Between! I have following one. It is working i.e. encoding original file and then decoding back to original file as teacher was saying one's complement will encode and then again one's complement will decode.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
FILE *fp, *ft, *fs;
char ch;
fp = fopen("original.txt","r");
ft = fopen("encode.txt","w+");
fs = fopen("decode.txt","w");
if (fp == NULL)
{
printf("File can't be opened\n");
exit(0);
}
if (ft == NULL)
{
printf("File can't be opened\n");
exit(0);
}
if (fs == NULL)
{
printf("File can't be opened\n");
exit(0);
}
while((ch = getc(fp)) != EOF)
{
putc(~ch , ft);
// printf("%c", ch);
// system("pause");
}
fclose(fp);
fclose(ft);
//remove("original.txt");
//rename("original.txt", "encode.txt");
fp = fopen("encode.txt", "r");
if(fp == NULL)
{
printf("File can't be opened\n");
exit(0);
}
while((ch = getc(fp)) != EOF)
{
/*printf("%c", ch); after encrypt data is not showing character ascii values so nothing display
encrypt data now may be in form of control commands or else.
To check if encryption is successful see the file in hard disk*/
putc(~ch , fs);
}
system("pause");
fclose(fp);
fclose(ft);
fclose(fs);
/*
Both r+ and w+ can read and write to a file. However, r+ doesn't delete the content of the file and doesn't create
a new file if such file doesn't exist, whereas w+ deletes the content of the file and creates it if it doesn't exist.
*/
}