Hi, I am trying to learn how to read the binary of a file, such as a jpg. I got something going here but at run time in windows it says there is an error and the file must close.
I'm not even sure if I'm going in the right direction here. What I want to do is read the file on a binary level so I can make my own algorithm to encrypt and decrypt it. I know there are a lot of programs out there for that but this is for educational purposes.
Whether it's right or wrong way for me, can anyone tell me the right way to get the binary into a vector and then tell me if I should be doing it another way?
Thanks
Here's the code I have so far. I'm sure there are some major problems since I have confused myself and got lost.
//test reading binary of file for encryption
// reading a complete binary file
#include <iostream>
#include <fstream>
#include <vector>
#include <stdio.h> //for atoi convert char to int
#include <stdlib.h>
using namespace std;
vector<int> data;
vector<int>::iterator iter;
int size;
char * memblock;
int main () {
ifstream file ("test.txt", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
for (int i = 0; i < size; ++i)
{
file.read (memblock, size);
//testing
cout << memblock;
//had error: invalid conversion from char to int
//and invalid conversion from char to char ? when using vector<char>
//so I did it this way to convert to int and use vector<int>
int newData = atoi (memblock);
data.push_back(newData);
}
file.close();
cout << "the complete file content is in memory";
}
else cout << "Unable to open file";
for (iter == data.begin(); iter != data.end(); ++iter)
cout << *iter;
cout << "\nPress Enter to exit.\n";
cin.ignore(cin.rdbuf()->in_avail() + 1);
return 0;
}