C ++创建二进制文件的读取,写入副本 [英] C++ create copy of binary file read, write

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

问题描述

Hallo社区,我正在尝试将可执行文件读入Char数组,然后使用该数组创建可执行文件。下面的我的代码读取和写入,但输出文件只有4Bytes。那么我应该修改什么。



Hallo community, i am trying to read an executable into an array of Char and then create an executable with the array. My Code below does read and write but the output file is only 4Bytes. So what should i modify.

ifstream in("F:\\helloworld.exe");		
		size_t len = in.tellg();
		char *oData = new char[len + 1];
		in.seekg(0, ios::beg);
		in.read(oData, len);
		oData[len] = '\0';
		 
		printf("%d", sizeof(oData));


		ofstream out("F:\\helloworld2.exe");
		out.write((char *)oData, sizeof(oData));
		out.close();

                delete[] oData;





MODIFIED:



我现在已将代码修改为以下内容,并且创建的文件具有相同的数量字节为原始。我删除了'\0'char,因为它似乎没必要。现在,当我运行克隆时,它显示程序太大而不适合内存,为什么?我希望我不要完全放弃我的脚。





MODIFIED:

I now have modified the Code to the following and the file is created with the same amount of Bytes as the original. I removed the '\0' char as it seems unnecessary. Now when i run the clone it displays "Program too big to fit in memory" and why? I hope im not going to blow my feet completely off.

ifstream in(path);	
		in.seekg(0, ios::end);
		size_t len = in.tellg();
		unsigned char *oData = new unsigned char[len];
		in.read((char*)(&oData[0]), len);
		in.close(); 
		
		size_t counted = in.gcount();
		cout << counted << endl; //result always 0 ???


		printf("\n %d", len); //same amount as original file length


		ofstream out(newP);	
		//myFile.open(newP, ios::out | ios::binary | ios::ate);
		out.write((char *)oData, len);
		out.close();


		delete[] oData;



为什么fread和fwrite不允许我分配 unsigned char 参数?



我尝试过:



问题和'你有什么尝试'必须至少30个字符。


And why does the fread and fwrite not let me assign an unsigned char parameter?

What I have tried:

Both the question and 'What have you tried' must be at least 30 characters.

推荐答案

oData是一个字符数组:所以它是一个指针到数组中的第一个char。由于您使用的是32位编译器(或至少生成32位代码),因此指针的大小为32位或4个字节。所以当你在任何指针上使用 sizeof 时,它将返回4的大小。



相反,使用 len 而不是 sizeof(oData) - 它已经是正确的长度,因为它是你创建数组时的大小!



并且不要使用 char 来保存二进制数据:这是一个七位值,二进制文件数据是八位宽。请改用 unsigned char
oData is an array of chars: so it's a pointer to the first char in the array. Since you are running with a 32 bit compiler (or at least producing 32bit code) the size of a pointer is 32 bits, or 4 bytes. So when you use sizeof on any pointer, it will return a size of 4.

Instead, use len instead of sizeof(oData) - it's the right length already, because it's the size of the array when you created it!

And don't use char to hold binary data: that's a seven bit value, and the binary data is eight bits wide. Use unsigned char instead.


您可以在阅读之前将文件指针设置为文件的开头。

May be you should set the file pointer to the beginning of file before reading.
in.seekg(0, ios::beg);
in.read(oData, len);



检查结果文件是否与原始文件相同。


Check if resulting file is the same as original one.


也许这不是确切的答案问题,但它确实有效。

代码来自StackOverflow,关于作者 Eutherpy

< a href =https://stackoverflow.com/questions/22129349/reading-binary-file-to-unsigned-char-array-and-write-it-to-another> c ++ - 将二进制文件读取到unsigned char数组并将其写入另一个 - Stack Overflow [ ^ ]



Maybe this is not the exact answer to the question but it does the job.
The code is taken from StackOverflow, with respect to the author Eutherpy
c++ - Reading binary file to unsigned char array and write it to another - Stack Overflow[^]

ifstream in(original_path, ios::binary);
		ofstream out(clone_path, ios::binary);
		if (in.is_open() && out.is_open())
			while (!in.eof())
				out.put(in.get());
		in.close();
		out.close();


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

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