I was creating a basic encryption Console program. and i found that after encrypting the file size was changed. it just increases the value by 3 of any character and writes to file as you can see it doesn't adds or removes any character. when decrypting the data the file size comes to normal the difference is before encrypting = 239 bytes after encrypting = 224 total difference = 15 bytes.
here is the code
I am not sure why the file size is different either, but I can offer some advice for improving your code.
In main, use a switch statement instead of a load of else if's. Also it's probably better style to put the code for each case: into functions. It's not a problem at the moment, but it will be if you have lots of code.
1 2 3 4 5 6 7 8 9 10 11 12 13
switch (choice) {
case 1:
DoEncryption();
break;
case 2:
DoDecryption();
break;
default:
printf("Bad Menu Option");
break;
}
Of course you will have to do forward declarations for you new functions and send them the arguments they need.
It's better style to do forward declarations for your functions rather than define them before main.
maybe use "wb" and "rb" instead of "w" and "r".. that will make sure the data is interpretated as binary data..
beside the main question if you have any advice for improving coding style then it's always welcome.
1 2 3 4 5
while(!feof(infile))
{
c = fgetc(infile);
fputc(c+3,outfile);
}
do not use a double feof check ;) You read the char and you check if reached the end after that, when the end of the file is reached the last character isn't written to the outfile....
maybe use "wb" and "rb" instead of "w" and "r".. that will make sure the data is interpretated as binary data..
thanks for advice. but i didn't used binary read and write options because i was dealing with text files. using binary messes up with the original text.
i also forgot to mention that i was using windows (my bad) . windows treats binary and text file differently. i will definitely use that binary option when i have to deal with binary files.
do not use a double feof check ;)
well i tried that thing and if i don't double check the position of the file it added 1 extra byte to the file. because fgetc moves to next offset (please fix me if i am wrong) after getting data.
it was going out side of file buffer so i needed that double check :P
and thanks TheIdeasMan for coding style it helps.
well i am still waiting for a solid answer about file size.
I also think that you should open the files in binary mode. Binary mode doesn't mean it can't be text. If you are not using binary mode on Windows it will automatically convert \r\n to \n when reading and \n to \r\n when writing. So what happens is that when your program reads a newline sequence from the file \r\n (2 characters) the program only receives a \n character. If you didn't encrypt the \n character it would just be converted to \r\n automatically when it was written to file and the file size would stay the same, but you encrypt it so it's no longer a \n character so no automatic conversion will take place so only one character is written to the file. That is why you get a different file size.