I wanted to read bytes from file and then rewrite them.
I did like so:
FILE *fp;
int cCurrent;
long currentPos;
/* check if the file is openable */
if( (fp = fopen(szFileName, "r+")) != NULL )
{
/* loop for each byte in the file crypt and rewrite */
while(cCurrent != EOF)
{
/* save current position */
currentPos = ftell(fp);
/* get the current byte */
cCurrent = fgetc(fp);
/* XOR it */
cCurrent ^= 0x10;
/* take the position indicator back to the last position */
fseek(fp, currentPos, SEEK_SET);
/* set the current byte */
fputc(cCurrent, fp);
}
After executing the code on a file, the size of the file is increasing within an infinite loop.
What is the problem in my code?
You are
XOR-ingcCurrentwith0x10even if it’s equal toEOF. Once youXOR, it’s no longerEOF, so your loop never terminates.Make the loop infinite, and exit from the middle when you see an
EOF, like this: