File Encryption & Read/Write in Binary Mode

Status
Not open for further replies.
Hi,

I am working on an encryption program. There are two versions. One is text encryption and the second is binary encrytion. The text encryption is fairly simply. The downside to the text version is that is only works on text files. One the other hand, I cannot get the binary encryption program to work.

There are two problems. First, I cannot copy complete raw data from any file and save it into a character array. I have tried giving the array a specific size and I have tried dynamically allocating the size of the array based on the size of the ifstream.

ifstream openFile;
openFile.open(x, ios::in | ios::binary);
openFile.seekg(0, ios::end);
size = openFile.tellg();

char *temp;
temp = new char(size);

openFile.read((char *) temp, sizeof(temp));

The code above does work, however, "sizeof(temp)" always output a 4 even if its subscript is 1000. the char * does not affect it because I saw the same number even if I use:

char temp[100];

How do you read raw data (binary) from any file and save it in memory so you can make specific encryption and then write it back in binary mode?

Thanks,
Kuphryn
 

un

Ars Praefectus
3,088
Subscriptor++
Off the top of my head, you need to make a couple of changes, first you've set the file pointer to the end of the file in order to get it's size, but you didn't return it to the start before reading, then you need to use size in your read call:

openFile.seekg(0, ios::beg);
openFile.read((char *) temp, size);

The sizeof thing is because sizeof() on a pointer returns the size of the pointer, not the item pointed to. On a 32bit machine this is typically going to be 4.
 
Status
Not open for further replies.