By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

file.read( (TCHAR*)&name_length, sizeof(unsigned int) );

P: n/a
This piece of code crashes my app:
basic_ifstream<TCHARfile(levelfilepath, ios_base::binary);
unsigned int name_length;
file.read( (TCHAR*)&name_length, sizeof(unsigned int) ); //crashes here -
file is open and contains binary data

It may take me hours to find out why this is incorrect, so if you know it,
please tell me.
Dec 28 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
of course TCHAR == wchar_t
Dec 28 '06 #2

P: n/a
r.z. wrote:
This piece of code crashes my app:
basic_ifstream<TCHARfile(levelfilepath, ios_base::binary);
unsigned int name_length;
file.read( (TCHAR*)&name_length, sizeof(unsigned int) ); //crashes here -
file is open and contains binary data

It may take me hours to find out why this is incorrect, so if you know it,
please tell me.
You have an error on line 42 of your code.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
Dec 28 '06 #3

P: n/a
Why? This is runtime error and I indicated the exact place where my app
crashes. Nothing more is necessary to tell whether this code is safe or not.
You have an error on line 42 of your code.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8


Dec 28 '06 #4

P: n/a

r.z. wrote:
This piece of code crashes my app:
basic_ifstream<TCHARfile(levelfilepath, ios_base::binary);
unsigned int name_length;
file.read( (TCHAR*)&name_length, sizeof(unsigned int) ); //crashes here -
file is open and contains binary data

It may take me hours to find out why this is incorrect, so if you know it,
please tell me.
Add the following code. It should help you debug the problem:

#include <assert.h>

assert(sizeof(unsigned int) == 4);
assert(sizeof(TCHAR) == 2);
unsigned int num_bytes_read = sizeof(unsigned int) * sizeof(TCHAR);
assert(num_bytes_read == 8);

unsigned int space_available_for_reading_into = sizeof(name_length);
assert(space_available_for_reading_into == 4);

assert(4 < 8);

If you're really reading a binary file, you probably shouldn't be using
TCHAR. In other words, if I recompile your code with Unicode
enabled/disabled, your code will have different behavior.

Michael

Dec 28 '06 #5

P: n/a
On Thu, 28 Dec 2006 21:23:47 +0100 in comp.lang.c++, "r.z."
<gk***@hjkjhk.plwrote,
>of course TCHAR == wchar_t
Well, there is your problem. Change TCHAR to (unsigned char).
You are reading (sizeof TCHAR)*(sizeof int) bytes into a variable
that is only (sizeof int).

By the way, "TCHAR" is probably a bad choice of a name, since you will
find Microsoft using it all over much of their code. Even though you
may have nothing to do with Windows systems, you are likely to run into
a conflict sooner or later -- this problem may be it! Just as you would
never start a class name with "C" as they have de-facto reserved that
letter for Microsoft classes.

Dec 28 '06 #6

P: n/a
On 28 Dec 2006 13:37:40 -0800 in comp.lang.c++, "Michael"
<mc******@aol.comwrote,
>assert(4 < 8);
Should be:
assert(4 == 8);

Dec 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.