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

Discussion in 'C++' started by Skywise, Aug 22, 2004.

  1. Skywise

    Skywise Guest

    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?
    Skywise, Aug 22, 2004
    #1
    1. Advertising

  2. Skywise

    David Hilsee Guest

    "Skywise" <> wrote in message
    news:...
    > 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
    David Hilsee, Aug 22, 2004
    #2
    1. Advertising

  3. Skywise

    David Hilsee Guest

    "David Hilsee" <> wrote in message
    news:eek:...
    <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
    David Hilsee, Aug 22, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Brewer
    Replies:
    1
    Views:
    478
    bsmith
    Nov 7, 2004
  2. Thomas Guettler

    assert 0, "foo" vs. assert(0, "foo")

    Thomas Guettler, Feb 23, 2005, in forum: Python
    Replies:
    3
    Views:
    2,497
    Carl Banks
    Feb 23, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,756
    Smokey Grindel
    Dec 2, 2006
  4. Alex Vinokur

    assert(x) and '#define ASSERT(x) assert(x)'

    Alex Vinokur, Nov 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    890
    Keith Thompson
    Nov 25, 2004
  5. ImpalerCore

    To assert or not to assert...

    ImpalerCore, Apr 27, 2010, in forum: C Programming
    Replies:
    79
    Views:
    1,620
    Richard Bos
    May 17, 2010
Loading...

Share This Page