I'm making a program to sum a char from a file with a random number and then subtract to get the file again.
The problem is when I sum them and subtract, the resulting file is not the same.
Can be bad math mine or something I don't know about chars.
Code to sum (edited):
while (!feof(filename))
{
size_t read = fread(&buf, sizeof(char), max_array, filename);
for (unsigned long long i = 0; i < read; i++)
{
unsigned char rdn = GetRandom_Soft();
/*
if (buf[i] + rdn > 255) buf[i] = (buf[i] + rdn) - 255;
else buf[i] = buf[i] + rdn;
*/
buf[i] = buf[i] + rdn;
num[i] = rdn;
}
fwrite(&num, sizeof(char), read, filename_n);
fwrite(&buf, sizeof(char), read, filename2);
}
Code GetRandom()
(edited):
int GetRandom_Soft()
{
return rand() % 256;
}
Code to subtract (edited):
while (!feof(filename2))
{
size_t read_c = fread(&num, sizeof(char), max_array, filename_n);
fread(&buf, sizeof(char), max_array, filename2);
for (unsigned long long i = 0; i < read_c; i++)
{
buf[i] = buf[i] - num[i];
/*
if(buf[i] - num[i] < 0) buf[i] = (buf[i] - num[i]) +255;
else buf[i] = buf[i] - num[i];
*/
}
fwrite(&buf, sizeof(char), read_c, filename);
}
*********Update 1*********
filename
= File pointer to the original file
filename_n
= File pointer to the file with the numbers
filename2
= File pointer to the file with the sum of filename
and filename_n
********Update 2(edited)*********
Declaration of chars array:
int num[9999]
int buf[9999]
int rdn[9999]