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

S

Skywise

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?
 
D

David Hilsee

Skywise said:
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".
 
D

David Hilsee

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top