Click here to Skip to main content
15,920,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C++
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:

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;

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.
Posted
Updated 10-Jun-17 15:03pm
v3

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.
 
Share this answer
 
Comments
[no name] 10-Jun-17 12:27pm    
Ok i wondered why one in C++ doesn't use a Byte array for writing like in .NET so in this case i have to use unsigned char, thanks for the tip. I'll give my code a try and will show result.
[no name] 10-Jun-17 13:04pm    
Please consider my improved question.
May be you should set the file pointer to the beginning of file before reading.
C++
in.seekg(0, ios::beg);
in.read(oData, len);

Check if resulting file is the same as original one.
 
Share this answer
 
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();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900