Hello, this is my first post
I've done a simple file encryption program, but it takes too much cpu power and i will appreciate any advice you can give me to optimise my code. This is the part that needs to be improved:
Code:
while(1){
err = fread( &byte_buff , sizeof(unsigned char) , 1 , input_file );
if( err != 1 ){
if( feof( input_file ) ){
printf("\n\n Operation successful!\n");
break;
}
else{
fprintf(stderr, "\n Error occured while reading from file!!!\n");
break;
}
}
//-- Encrypt / Decrypt the byte with Bitewise NOT
byte_buff = ~ byte_buff;
err = fwrite( &byte_buff , sizeof(unsigned char) , 1 , output_file);
if( err != 1 ){
fprintf(stderr, "\n Error occured while writing!!!\n");
break;
}
if( (i % ( file_size / 20 ) == 0) && (i!=0) ){
printf("\b\b%d", percentage += 5); fflush(stdout);
}
i++;
}
Also i have a question about fread() and fwrite(). Is there a better way than read/write one byte at a time so i can boost the speed ? Because if i read more than 1 byte, lets say 2 but the file is 3 bytes, i'm not sure if fread() will read the last byte and when reach eof stop, or it will give me the last byte + 1 extra junk byte
English is not my native language, so excuse me for my mistakes