Read exe file contents to wstring in C++?

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>
using namespace std;
wstring readfile(const char* 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.
It is possible only with std::string, not with std::wstring when working with binary files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	ifstream fs;
	fs.open ("test.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 = new char[i];
	fs.seekg(0, ios::beg);
	fs.read (buf, i);
	fs.close();
	string s;
	s.assign (buf, i);
	delete [] buf;


	// access 's' std::string here
	cout << s.length() << endl;

	return 0;
}
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?

Andy

PS Note that it's only with C++11 that std::string/std::wstring is actually guaranteed to store the data contiguously (allowing the copy approach). But it is already a de-facto standard, apparently. For Herb Sutter's comments on the matter, see http://herbsutter.com/2008/04/07/cringe-not-vectors-are-guaranteed-to-be-contiguous/#comment-483
Last edited on
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?
Here is the code that uses vector to read a file..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    #include <sstream>
    #include <fstream>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    vector<char> readfile(const char* filename)
    {
    ifstream infile(filename,ios::binary);
    vector<char> result;
    copy(istream_iterator<char>(infile),
    istream_iterator<char>(),
    back_inserter(result));
    return result;
    }
    int main()
    {
    vector<char> data = readfile("hello.exe");
    cout << data.size() << endl;//how do i edit the data read from exe file?
    return 0;
    }
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.
Last edited on
a Unicode char (well, wchar_t) is twice as big as a char.

This is only true on Windows (as far as I know). On most other systems, wchar_t is four times as big as a char.
I admit I assumed the Microsoft definition of both wchar_t and Unicode was ok to use here. After all, this is the Windows forum.

I have inserted "in the case of Microsoft Windows" into my earlier post, in case someone from Linux-land visits this forum...
Topic archived. No new replies allowed.