char * buffer;
long size;
ifstream infile ("old.exe",ifstream::binary);
ofstream outfile ("new.exe",ofstream::binary);
// get size of file
infile.seekg(0,ifstream::end);
size=infile.tellg();
infile.seekg(0);
// allocate memory for file content
buffer = newchar [size];
// read content of infile
infile.read (buffer,size);
CString (str(buffer));
str.Replace("OLD","NEW");
// write to outfile
outfile.write(str,size);
// release dynamically-allocated memory
delete[] buffer;
outfile.close();
infile.close();
But when i cast from char array to CString all the information is lost (because of the presence of unicode characters)
i must cast to CString so that i can perform string replace function..
i have tried using _T() but it wont work..
please help
NOTE:I AM USING VC++ 6 MFC
std::wstring also has search/replace functions if your file is utf-16 le encoded. If it is not, but you know the encoding, use MultiByteToWideChar() API to convert (in memory) first to a Unicode (UTF-16) string.
Beware that you have a memory buffer there which is not null terminated.
wstring does not work either..
I can never be sure about the encoding.. because i want to replace strings in an exe file..
any idea what i need to do/read?
thanks for replying
If you want to "replace strings in a exe file", then just open a file in binary mode, copy the buffer to a std::string using assign() method (this can handle null characters just fine), perform the replace and write the string to a new file using std::ostream.write() method and std::string.data() as buffer.
Beware that this could give you unexpected results though, better use a resource editor.
thanks modoran.. now i have an idea of where to work on..
yeah, i have done this before using char array and alot of if statements.. :D
just wanted to follow a better method by using replace function but so far i am not able to read/copy the buffer to a string/wstring/cstring because of the null characters present in an exe file..
i hope the assign method will work fine.. i will return soon after trying it out.. :)
Regards
i don't want to use third party software, because i am creating an MFC application that replaces strings of exe files..
like in a "Hello World" program, change "Hello" to "Screw"..
#include <fstream>
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
ifstream fs;
fs.open ("1.exe", ios::binary);
if (fs.fail())
{
cerr << "Failed to open file!\n";
return 1;
}
fs.seekg(0, ios::end);
size_t i = fs.tellg();
char* buf = newchar[i];
fs.seekg(0, ios::beg);
fs.read (buf, i);
fs.close();
string s;
s.assign (buf, i);//is this the right way to assign
delete [] buf;
cout << s.data() << endl;//same old problem only a few chars printed
return 0;
}
How do you know line 22 is the problem? Isn't s.size() same as i?
If s.size() == i I think that the problem is that std::cout can't handle the null characters. You could try using a loop to output all the characters in the string except the null characters.
1 2 3 4 5 6 7 8
for (char c : s)
{
if (c != '\0')
{
std::cout << c;
}
}
std::cout << std::endl;
well std::cout handles the null characters pretty well when i do this
1 2 3 4 5 6 7 8 9 10 11 12 13 14
ifstream fs;
fs.open ("1.exe", ios::binary);
if (fs.fail())
{
cerr << "Failed to open file!\n";
return 1;
}
fs.seekg(0, ios::end);
size_t i = fs.tellg();
char* buf = newchar[i];
fs.seekg(0, ios::beg);
fs.read (buf, i);
fs.close();
cout<<buf;//where buf is a char array
thanks for your time, could you think of any other way..?
also the code you gave does not work for me either.. which makes me think if i am some how able to store multiple NULL characters in a string someway then i can accomplish my goal..
thanks modoran for giving me the correct idea and Peter you too for pointing out the mistake (you were right about cout)
this codes works like a charm thank you guys, great to have you around