473,320 Members | 1,732 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.

Why am I getting an assert failure message with this code?

This function loads info from a binary data file, then loads objects
(currently 23 in the test file). Here is the loading function:

bool UnitDefLoad(char* sFile)
{
UnitDefCleanList(false); // tested. This works.
int iUnitCount = 0;
UNIT_DEF uData; // Theses objects are read from file
ifstream fin(sFile,ios::binary);
if (!fin)
{
return false;
} // end if
else
{
fin.read((char*) &Unit_Def_Node::Arthur, sizeof
(Unit_Def_Node::Arthur));
fin.read((char*) &Unit_Def_Node::iMonth, sizeof
(Unit_Def_Node::iMonth));
fin.read((char*) &Unit_Def_Node::iDay, sizeof
(Unit_Def_Node::iDay));
fin.read((char*) &Unit_Def_Node::iYear, sizeof
(Unit_Def_Node::iYear));
fin.read((char*) &Unit_Def_Node::Description, sizeof
(Unit_Def_Node::Description));
fin.read((char*) &Unit_Def_Node::iVersion, sizeof
(Unit_Def_Node::iVersion));
fin.read((char*) &Unit_Def_Node::iBuild, sizeof
(Unit_Def_Node::iBuild));
fin.read((char*) &iUnitCount, sizeof (iUnitCount));

for (int iCount = 1; iCount < iUnitCount; iCount++)
{
fin.read((char*) &uData, sizeof (uData));
UnitDefADD(uData);
cout << "Number of Unit_Def_Nodes: " << iUnitsDefined << "\n";

}; // end for
};
fin.close();
return true;
};
I believe the problem is the
fin.read((char*) &uData, sizeof (uData));
in the for loop. I have 23 UNIT_DEF objects saved in the file. After
the 23rd object is loaded, it exits the loop and crashes... if I
comment out the above line, the program works fine. Is there
something wrong with my syntax?
Jul 22 '05 #1
2 1167
"Skywise" <th********@houston.rr.com> wrote in message
news:21**************************@posting.google.c om...
This function loads info from a binary data file, then loads objects
(currently 23 in the test file). Here is the loading function:

bool UnitDefLoad(char* sFile)
{
UnitDefCleanList(false); // tested. This works.
int iUnitCount = 0;
UNIT_DEF uData; // Theses objects are read from file
ifstream fin(sFile,ios::binary);
if (!fin)
{
return false;
} // end if
else
{
fin.read((char*) &Unit_Def_Node::Arthur, sizeof
(Unit_Def_Node::Arthur));
fin.read((char*) &Unit_Def_Node::iMonth, sizeof
(Unit_Def_Node::iMonth));
fin.read((char*) &Unit_Def_Node::iDay, sizeof
(Unit_Def_Node::iDay));
fin.read((char*) &Unit_Def_Node::iYear, sizeof
(Unit_Def_Node::iYear));
fin.read((char*) &Unit_Def_Node::Description, sizeof
(Unit_Def_Node::Description));
fin.read((char*) &Unit_Def_Node::iVersion, sizeof
(Unit_Def_Node::iVersion));
fin.read((char*) &Unit_Def_Node::iBuild, sizeof
(Unit_Def_Node::iBuild));
fin.read((char*) &iUnitCount, sizeof (iUnitCount));

for (int iCount = 1; iCount < iUnitCount; iCount++)
{
fin.read((char*) &uData, sizeof (uData));
UnitDefADD(uData);
cout << "Number of Unit_Def_Nodes: " << iUnitsDefined << "\n";

}; // end for
};
fin.close();
return true;
};
I believe the problem is the
fin.read((char*) &uData, sizeof (uData));
in the for loop. I have 23 UNIT_DEF objects saved in the file. After
the 23rd object is loaded, it exits the loop and crashes... if I
comment out the above line, the program works fine. Is there
something wrong with my syntax?


I don't see anything wrong with your syntax, but the syntax is not where the
problem lies. If you don't supply a complete example, it is hard for anyone
to help you. I doubt that anyone else browsing this group knows what
Unit_Def_Node is, what a UNIT_DEF is, what UnitDefADD does, what the file
contains, etc. The best advice you can get from your current post is "use a
debugger".

--
David Hilsee
Jul 22 '05 #2

"David Hilsee" <da*************@yahoo.com> wrote in message
news:ob********************@comcast.com...
<snip>
I don't see anything wrong with your syntax, but the syntax is not where the problem lies. If you don't supply a complete example, it is hard for anyone to help you. I doubt that anyone else browsing this group knows what
Unit_Def_Node is, what a UNIT_DEF is, what UnitDefADD does, what the file
contains, etc. The best advice you can get from your current post is "use a debugger".


The second best advice would be to check to see if the calls to read()
actually succeed. The third best would be to use a text file instead of a
binary file so it's easier to follow.

--
David Hilsee
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
11
by: David | last post by:
I am compiling C++ under both MacOsX and under Bloodshed Dev C++ on Windows XP. Because the console window disappears under Bloodshed when using the standard assert() via #include <cassert> I...
47
by: Rob Thorpe | last post by:
In general, is it considered bad practice to use asserts in production code? What about writing a macro that does the same as assert but continues to work regardless of the state of NDEBUG? I...
3
by: sundew | last post by:
so here is the test case: <head> <script type='text/javascript'> // this function simply returns the number of enumerable namespaces(objects) function numNSpaces(){ var num = 0; for(var i in...
8
by: priyanka | last post by:
Hi there, Can anyone show me how the assert() function works ? I need to develop my own assert() function instead of using the one defined in the assert.h file. It would be great if anyone could...
29
by: mailforpr | last post by:
Sometimes, I can't think of any good reason why I should have the program's logic thrown an exception. Except for catching the exception and printing "Uh, oh" to the screen. I also think that in...
10
by: Paul Rubin | last post by:
So I have some assert statements in my code to verify the absence of some "impossible" conditions. They were useful in debugging and of course I left them in place for "real" runs of the program. ...
30
by: Tomás Ó hÉilidhe | last post by:
In C89, do we have to pass an int as an argument to assert? I've got code at the moment that does an assertion on pointer, e.g.: assert(p); , but I'm wondering if I should change that to: ...
32
by: bingfeng | last post by:
hello, please see following two code snatches: 1. int foo (const char* some) { assert(some); if (!some) { return -1; //-1 indicates error }
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.