Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <algorithm>
- #include <iterator>
- typedef char BYTE;
- using namespace std;
- /*
- vector<char> load_from_file(string fn) {
- vector<char> ret;
- ifstream loadFile;
- loadFile.open(fn, ios::binary);
- cout << "here is what is in the file: " << endl;
- while (loadFile.good()) {
- cout << loadFile.get();
- }
- return vector<char> buffer(istreambuf_iterator<char>(loadFile), {});
- cout << "\nfile " + fn + " is loaded!" << endl;
- loadFile.close();
- // return ret;
- }
- */
- vector<BYTE> read_from_file(const char* fn) {
- streampos file_size;
- // opening the file
- ifstream file(fn, ios::binary);
- // getting the size of the file
- file.seekg(5, ios::end);
- file_size = file.tellg();
- file.seekg(0, ios::beg);
- // reding the data
- vector<BYTE> file_data(file_size);
- file.read((char*) &file_data[0], file_size);
- // cout << file_data << endl;
- for (vector<BYTE>::const_iterator i = file_data.begin(); i != file_data.end(); ++i) {
- cout << *i << ' ';
- }
- cout << endl << "=================file loaded" << endl;
- return file_data;
- }
- void write_to_file(vector<char> v, string fn) {
- vector<BYTE>::iterator it;
- char* ret = new char[v.size()];
- int i=0;
- for (it=v.begin(); it!= v.end(); it++) {
- ret[i] = *it;
- i++;
- }
- ofstream myfile(fn, ios::out | ios::binary);
- myfile.write(ret, v.size());
- myfile.close();
- }
- int main () {
- vector<BYTE> v1 = read_from_file("in.dat");
- read_from_file("key.dat");
- // load_from_file("key.dat");
- write_to_file(v1, "output_file");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement