Trying to write 4 bytes ints in a binary file and extract them after... I'm using the exclusive or (^) to isolate single bytes to write to and extract from the file since the write() function accepts only chars, only the beginning and end results are not the same... what am I doing wrong?
I've implemented my own method to store the data, so it's working now, but I still don't understand why it's not working with << and >>... anyway the whole code is there... try it if you want ! Or how would you do it if you had to store ints in chars in a file...? :-)
The point is that xor encrypts data. You need to do the exact the same thing in reverse to decrypt it again. If you don't want encryption/decryption xor is useless and you can simply omit it.
store ints in chars in a file...?
If there is no good reason to do it otherwise: simply convert them from/to string. [f]stream is doing it for you with their overloaded << >> operator. Note: the stream operators are not binary shift operators.
Write:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
if(map_template.is_open())
{
cout << "file opened...";
for(unsignedshort i = 0; i < NUMBER_OF_RANDOM_NUMBERS; i++)
{
map_template << rand_numbers[i] << ' '; // Note: ' ' to separate numbers
}
map_template.close();
cout << "done" << endl << endl;
}
else
{
cout << "file is not opened, exit" << endl << endl;
return 0;
}
Read:
1 2 3 4 5 6 7 8 9
fstream ifs(...); // Note
int results[NUMBER_OF_RANDOM_NUMBERS];
index = 0;
while(ifs >> results[index])
{
++index;
}