1

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]

Samega 7Cattac
  • 253
  • 1
  • 3
  • 16

1 Answers1

2

GetRandom_Soft() returns char, whose value range is [-128;127], and you try to assign values from the range [0 ; 255] - not gonna work properly.

I also suspect that you're doing similar things here:

buf[i] = buf[i] + rdn;

and here:

buf[i] = buf[i] - num[i];

I suspect that either buf or num or both are of different types than char* (e.g. unsigned char*) AND/OR the result of the addition or the subtraction is outside of the char value range which corrupts the result.

EDIT

After your update it's almost certainly the problem with unsigned char overflow. unsigned char takes value from range [0;255] - if the result of your addition is > 255 or the result of your subtraction is < 0, than you're going to get erroneous results. It seems that interpreting the values as int makes much more sense in case of your numbers.

KjMag
  • 2,650
  • 16
  • 16
  • Good to know that! but I try to add `unsigned` to the chars declaration and that don't solve the problem, but is more logic to have it, so I add it and updated the question. – Samega 7Cattac Jul 10 '17 at 09:52
  • And of corse add 255 if subtraction < 0 – Samega 7Cattac Jul 10 '17 at 10:28
  • 1
    If you keep using unsigned char, you won't be able to detect that sum < 0 or sum > 255. The result is going is converted to unsigned char, so it's goint to still be in range [0;255]. Read this topic to get a better understanding: https://stackoverflow.com/questions/21530358/how-256-stored-in-char-variable-and-unsigned-char Just use ints and you won't have to worry about your value range unless your numbers are huge. – KjMag Jul 10 '17 at 10:31
  • so I can't use `unsigned char` for any of char declarations, right? Is better to use int? – Samega 7Cattac Jul 10 '17 at 10:41
  • Ok, using `int` instead of `unsigned char` almost worked, I can now see some part with the original chars, but few – Samega 7Cattac Jul 10 '17 at 11:13
  • I recognized some char in the file, but never mind already solved, later I will post my answer but this answer will be marked as response because of the link you posted I find out whats wrong. – Samega 7Cattac Jul 10 '17 at 14:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148791/discussion-between-kjmag-and-samega-7cattac). – KjMag Jul 10 '17 at 14:44