Problem Reading a file?

Hello, i am trying to read a file that contains Unicode Characters..
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
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 = new char [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
Last edited on
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
You have to know the encoding, otherwise there is no way you can handle it correctly.
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"..
copy the buffer to a std::string using assign() method

modoran, i have tried this code, but i cannot get it to work properly.. would you please correct me..
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
#include <fstream>
#include <iostream>
#include <string>
using namespace 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 = new char[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;
	
}

would you please provide a sample code
Last edited on
What happens if you change line 24 to cout << s << endl;?
same.. the problem is with casting from char * buf to string s (line 22) :s
i must find a way to read mutliple Null Characters into a string..
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;

Last edited on
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 = new char[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..
Last edited on
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
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
31
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
#include<windows.h>

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 = new char[i];
	fs.seekg(0, ios::beg);
	fs.read (buf, i);
	fs.close();
	string s;
	s.assign (buf, i);
	ofstream file;
	file.open("new.exe",ios::out);
	file.write(s.data(),s.size());//shoud i use s.size() or s.length()

	return 0;

	
}

please answer one last question, in the 2nd last line should i use size() or length?
is there anyother way i could make this code better?
thanks again
should i use size() or length?

Both do the same thing so it doesn't matter.
Topic archived. No new replies allowed.