I want to just create a small encryption program, i want to read characters from a file and just add the current value of i to the characters and store it in a new file.
example : abcd should be stored as aceg
Here is the code snippet:
main()
{
int i=0,c;
char filename[30],o_filename[30];
FILE *file,*outfile;
printf("\nEnter filename:");
scanf("%s",&filename);
printf("\nEnter output filename : ");
scanf("%s",&o_filename);
if( ( (file = fopen(filename,"a"))==NULL) || ((outfile = fopen(o_filename,"a+"))==NULL))
printf("\nERROR - Cannot proceed");
else
{
c = 0;
while(c!=-1)
{
c = getc(file);
putc((c+i),outfile);
i++;
}
printf("\nEncryption Successful!\n");
}
getch();
}
What’s the bug with the above program?
Th function
getcreturns anint, not achar.EOFcannot be represented in achar.Change the type of
ctointand test againstEOFin the loop.EDIT
To correct the off by one error spotted by marc you could try this idiom: