阅读文件的二进制 [英] Reading in a File's Binary

查看:314
本文介绍了阅读文件的二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在周围的Googling,不能锁定到什么,我试图做的。我得到的东西关于阅读了二进制文件,但在这些链接,人们一提起头和格式,这似乎相反的是我了。

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).

首发目标:在任何扩展成C ++向量(或其他 - 如果事情是优越的,我为建议)中的任何文件读取,然后重写相同的文件到硬盘驱动器以不同的名称。以后,我要检查,看是否该文件仍然有效/是相同尺寸的/ etc。

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.

所以我想确保我有一个文件会吸进'A',并能够写入文件一旦它在'A'之前,我拼接起来'A'的内容,并且将其放入的能力图像。

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.

感谢您的引用。

推荐答案

有更好的(和更有效)的方式来复制文件。但是回答您的具体问题:

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());
   }
}

这篇关于阅读文件的二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆