i have been stuck with this for days, hope i can get some help from you guys.. i am new here..
here is the code that reads contents of exe file to a stream..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <sstream>
#include <fstream>
#include<iostream>
usingnamespace std;
wstring readfile(constchar* filename)
{
wifstream wif(filename,ios::binary);
wstringstream wss;
wss << wif.rdbuf();
return wss.str();
//is there a way i can perform search operations on rdbuf()?
}
void main()
{
wstring wstr = readfile("hello.exe");
wcout<<wstr;//here is get only 3 characters =(
}
i think the reading operation is terminated upon the first occurrence of NULL character, how can i stop this from happening.
and yeah, i know how to read file to a char array, but that is not what i want. i want to perform search operations which is much easier using strings.
one more thing, i have tried to read the file into char array and then cast char array to wstring but again only some characters are stored to wstring.
Thanks for answering, it helped me. Is there anything i can do regarding wstring?
because the file i want to read consists of unicode characters..
thanks again =)
You mention that you want to read an exe file. While it might contain Unicode strings, it's not going to be just Unicode strings. So trying to treat it as a Unicode string is doomed to failure.
It is probably possible to pre-allocate the string buffer and copy the data across, but if the data is not in the right format this would be a waste of time. Or is the exe special in some way?
Also, you are talking about string literals in the code, rather than the exe's resource segment?
Thanks for answering Andy. i can never be sure of the format of data..
Let me elaborate a little bit, say i have made a hello world program and i want to read its entire exe.. find the string "Hello World" among a lot of strange characters and replace it with "Mad World"..
i have done this successfully using the following simple algorithm.
read file to char*
in a 'for loop' search for the world "hello"
replace it with Mad..
To make search/replace operations easier i was trying to accomplish this task using strings.
From another source i have found that i can do this using Vector char.. what you think about that?
I would use code more like modoran's above, exploiting the fact that, in the case of Microsoft Windows, a Unicode char (well, wchar_t) is twice as big as a char. Then, if you're into STL algorithms, you could use search, etc to do the swap.
But the standard practice for strings which need to be replaced is to use some kind of localization mechanism or config file. What you're trying to achieve is rather non-standard!
Andy
PS Note that in the Windows world, Unicode and wchar_t refer to 16-bit, UTF-16 encoded chars and strings. Which is not always the case in the rest of the cosmos.