Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <iterator>
  6.  
  7. typedef char BYTE;
  8.  
  9. using namespace std;
  10. /*
  11. vector<char> load_from_file(string fn) {
  12.     vector<char> ret;
  13.     ifstream loadFile;
  14.  
  15.     loadFile.open(fn, ios::binary);
  16.  
  17.     cout << "here is what is in the file: " << endl;
  18.  
  19.     while (loadFile.good()) {
  20.         cout << loadFile.get();
  21.     }
  22.  
  23.    return vector<char> buffer(istreambuf_iterator<char>(loadFile), {});
  24.  
  25.  
  26.     cout << "\nfile " + fn + " is loaded!" << endl;
  27.     loadFile.close();
  28.  
  29.     // return ret;
  30. }
  31.  
  32.  
  33.  
  34. */
  35.  
  36.  
  37. vector<BYTE> read_from_file(const char* fn) {
  38.     streampos file_size;
  39.  
  40.     // opening the file
  41.     ifstream file(fn, ios::binary);
  42.  
  43.     // getting the size of the file
  44.     file.seekg(5, ios::end);
  45.     file_size = file.tellg();
  46.     file.seekg(0, ios::beg);
  47.  
  48.     // reding the data
  49.     vector<BYTE> file_data(file_size);
  50.     file.read((char*) &file_data[0], file_size);
  51.     // cout << file_data << endl;
  52.     for (vector<BYTE>::const_iterator i = file_data.begin(); i != file_data.end(); ++i) {
  53.         cout << *i << ' ';
  54.     }
  55.     cout << endl << "=================file loaded" << endl;
  56.     return file_data;
  57.  
  58. }
  59.  
  60. void write_to_file(vector<char> v, string fn) {
  61.     vector<BYTE>::iterator it;
  62.     char* ret = new char[v.size()];
  63.     int i=0;
  64.     for (it=v.begin(); it!= v.end(); it++) {
  65.         ret[i] = *it;
  66.         i++;
  67.     }
  68.     ofstream myfile(fn, ios::out | ios::binary);
  69.     myfile.write(ret, v.size());
  70.     myfile.close();
  71. }
  72.  
  73.  
  74. int main () {
  75.     vector<BYTE> v1 = read_from_file("in.dat");
  76.     read_from_file("key.dat");
  77.     // load_from_file("key.dat");
  78.     write_to_file(v1, "output_file");
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement