473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
6 3281
of course TCHAR == wchar_t
Dec 28 '06 #2
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jaap de Bergen | last post by:
Hello people, I'm optimizing my script because my server is under heavy load in the evening. I have a question which methode is more efficient for my server: ==================...
2
by: Boris Boutillier | last post by:
HI all, I came across a strange behaviour with read method, and I'm not sure if this is a filesystem problem or a misuse of this method. I do the simple following thing : f = open...
3
by: Tony Murphy | last post by:
Strange problem, I read a file into a string using ifstream, ostringstream and string and end part of the file is missing (file size ~9.5kb, ~9k read). its a html file. using windows nt 4 ...
7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
2
by: js | last post by:
Hi list. I'm writing a tail -f like program in python and I found file.read() doesn't work as I think it should. Here's the code illustrating my problem. ### #!/usr/bin/env python import...
2
by: Prateek | last post by:
I have a wierd sort of problem. I'm writing a bunch of sets to a file (each element is a fixed length string). I was originally using the built-in sets type but due to a processing issue, I had...
7
by: Marcus Kwok | last post by:
I am working on a program that reads and processes large text files (on the order of 32 MB, so not too huge), so I wanted to add a progress indicator so I can estimate when it will finish. I just...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.