-1

I have been Googling around, and cannot lock on to what I am trying to do. I get things about reading a 'binary file' but in these links, people mention headers and formatting, which seems contrary to what I am after.

end goal: encrypt any file in a picture (assuming the picture is big enough for the file).

starting goal: read in any file of any extension into a c++ vector (or whatever -- if something is superior, I am up for suggestions) and then rewrite that same file onto the hard drive under a different name. After, I want to check to see if the file still works/is the same size/etc.

So I am trying to make sure I have the ability to suck in a file into 'A' and be able to write a file once it's in 'A' before I splice up the contents of 'A' and stick it into an image.

Thank you for your references.

user904963
  • 1,365
  • 2
  • 11
  • 22
  • possible duplicate of [Reading and writing binary file](http://stackoverflow.com/questions/5420317/reading-and-writing-binary-file) – FailedDev Nov 30 '11 at 18:03
  • 1
    Why oh why would you ever read binary data into a C++ vector? An allocated chunk of memory (or even better a memory map) is more than sufficient. – Chris Eberle Nov 30 '11 at 18:03
  • If you read a file into a buffer and then write the entire buffer to a new file, then the size and content would be the same, or else you are doing something wrong=) – Cyclonecode Nov 30 '11 at 18:03
  • I don't think the poster's problem is binary vs text files - I think they are talking about image files – Martin Beckett Nov 30 '11 at 18:05
  • "Starting goal" is an oxymoron. Where are you getting stuck? – Potatoswatter Nov 30 '11 at 18:11
  • Starting goal is not an oxymoron. A goal is a target, and any reasonable person breaks up a larger goal into many smaller goals. I am getting stuck with what functions or tools to look into for this operation, or else I would have posted code with my specific errors flagged. – user904963 Nov 30 '11 at 18:19
  • @Potatoswatter the above – user904963 Nov 30 '11 at 19:59

2 Answers2

2

You are asking for references to something called Steganography. I have written a research paper on it. Short of posting it in full here, Wikipedia has a page about it.

Joe McGrath
  • 1,413
  • 10
  • 25
0

There are better (and more efficient) ways to copy a file. However to answer your specific question:

#include <fstream>
#include <vector>

int main()
{
   std::ifstream in("input_file", std::ios::binary);

   auto beg = in.tellg();
   in.seekg(0, std::ios::end);
   auto end = in.tellg();

   auto sz = end - beg;

   std::vector<char> outbuf;

   if(0 != sz)
   {
      in.seekg(0, std::ios::beg);
      outbuf.resize(sz);

      in.read(&outbuf[0], outbuf.size());

      std::ofstream out("output_file", std::ios::binary);
      out.write(&outbuf[0], outbuf.size());
   }
}
Chad
  • 17,081
  • 2
  • 40
  • 60
  • This is what I'm looking for, but it doesn't seem to be working. I used test.rar (size 333 bytes -- a rar'ed txt file). output_file was 133kb and fails to extract using winrar (after being renamed test.rar) – user904963 Nov 30 '11 at 19:17
  • Just a simple test on my end and it worked as expected. Using multi-byte (not unicode) encoding. Sorry its not working for you. – Chad Nov 30 '11 at 19:20
  • http://pastebin.com/YtG52jP3 That is my code. I type in the console "test.rar" and have a test.rar file ready to be read. Did I change something in your code to make it not work? My compiler didn't like auto beg etc. – user904963 Nov 30 '11 at 19:28
  • No your pastebin code looks fine. The `auto` keyword is C++11. – Chad Nov 30 '11 at 19:40
  • Do you think it has to do with the type of 'encoding'? I'm running Windows XP here, but the code needs to run on Linux for the final submission (it will be compiled using g++ there). – user904963 Nov 30 '11 at 19:51
  • Potentially. When you debug through the code what does BUFFER_SIZE come out to be? – Chad Nov 30 '11 at 20:00
  • It is a weird variable unlike an int. It has _stz 0 _Myoff 136704 _Fpos = 0 _Mystate = 0 – user904963 Nov 30 '11 at 20:07
  • I just outputted the size of my output buffer, and it is 136704, meaning it's using that value of the four. – user904963 Nov 30 '11 at 20:10
  • Holy smokes, I just added for(vector::const_iterator itr = outputBuffer.begin(); itr != outputBuffer.end(); ++itr) cout << *itr; and it outputs "this program cannot be ran in dos" and starts making beep noises repeatedly as it outputs the chars it got from the file. – user904963 Nov 30 '11 at 20:20
  • I solved the problem. I was using [0] instead of [1] when naming the file. Thanks for your help. – user904963 Nov 30 '11 at 22:46