Thread: fstream problem

  1. #1
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320

    fstream problem

    What would cause fstream to read in crazy stuff? It works the first 80 or so times fails eventually, which is unacceptable.

    Code:
    ifstream load_file;
        load_file.open(filename.c_str());
    
        load_file>>temp>>W_num_frames;
        W_ANI.assign(root_path);
        W_ANI.append(temp);
    
        load_file>>temp>>A_num_frames;
        A_ANI.assign(root_path);
        A_ANI.append(temp);
    
        load_file>>temp>>D_num_frames;
        D_ANI.assign(root_path);
        D_ANI.append(temp);
    
        rm->Load_Sprite(W_ANI,W_ANI);
        rm->Load_Sprite(A_ANI,A_ANI);
        rm->Load_Sprite(D_ANI,D_ANI);
    My frames number will read in as some huge negative number and temp in all cases will be nothing so I end up with the root_path and that is it. Would using load_file.bad() be an option here? What could be causing my issue with reading a small text file?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Describe your data.

    Also, what do you mean that it "works for the first 80 or so"? The first 80 what? Independent runs of the program on the same data? Iterations of a loop in a single run?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Well this is a monster class that loads monster data from a file the file contains simple data paths for the animations and such. I can spawn 80 monsters or so shoot emm up and whatnot, but then all of a sudden fstream doesnt populate temp and my frames count which causes the program to crash atm.

    this is the file:
    Code:
    Spider_Walk/
    12
    Spider_Attack/
    1
    Spider_Die/
    9
    
    3
    17 32 5  2
    28 32 8  1
    48 32 15 1

    This is my constructor at the moment:
    Code:
    Monster::Monster(string filename,Resource_Manager *nrm)
    {
        rm = nrm;
        //Class initalizations
        Draw_Hit_Zones = true;
        Draw_Health_Bar = true;
        last_zone_hit = -1;
        Dieing = false;
        Time_Of_Death = 0;
    
        cur_state = 0;
        MAGIC = NULL;
        delay = 100;
        cur_frame = 0;
        dr = 0;
        r = 0;
        Move_Speed = 10;
        timer = clock();
    
        Max_Hit_Points = 50;
        Cur_Hit_Points = Max_Hit_Points;
        //end class initalizations
    
        stringstream ss;
        string root_path = filename.substr(0,filename.find_last_of("/\\")+1);     // /gfx/Spider/
    
        string load_image_path;
        string load_mask_path;
    
        string temp;
    
        ifstream load_file;
        load_file.open(filename.c_str());
    
        load_file>>temp>>W_num_frames;
        W_ANI.assign(root_path);
        W_ANI.append(temp);
    
        load_file>>temp>>A_num_frames;
        A_ANI.assign(root_path);
        A_ANI.append(temp);
    
        load_file>>temp>>D_num_frames;
        D_ANI.assign(root_path);
        D_ANI.append(temp);
    
        rm->Load_Sprite(W_ANI,W_ANI);
        rm->Load_Sprite(A_ANI,A_ANI);
        rm->Load_Sprite(D_ANI,D_ANI);
    
        magic_number = (int)ceil(sqrt(rm->Get_Sprite(W_ANI,0)->w*rm->Get_Sprite(W_ANI,0)->w + rm->Get_Sprite(W_ANI,0)->h*rm->Get_Sprite(W_ANI,0)->h));
    
        load_file>>num_col;
    
        Hit_Zones = new C_Circ*[num_col];
        multipliers = new int[num_col];
        int cx,cy,cr;
        for(int lcv = 0;lcv < num_col;lcv++)
        {
            load_file>>cx>>cy>>cr>>multipliers[lcv];
            Hit_Zones[lcv] = new C_Circ(cx+(magic_number-rm->Get_Sprite(W_ANI,0)->w)/2,cy+(magic_number-rm->Get_Sprite(W_ANI,0)->h)/2,cr);
        }
        Master_Hit_Zone = new C_Rect(x - (rm->Get_Sprite(W_ANI,0)->w/2),y - (rm->Get_Sprite(W_ANI,0)->h/2),rm->Get_Sprite(W_ANI,0)->w,rm->Get_Sprite(W_ANI,0)->h);
        load_file.close();
    }
    Last edited by ~Kyo~; 03-25-2012 at 07:42 PM.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    So you can read about 80 separate "monster" files but after that they start screwing up? It would seem that the problem is not in the Monster ctor then.

    How are you maintaining the "list" of monsters?

    BTW, dieing is spelled dying (if you mean death as opposed to dice or machine stamping/cutting).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Since when does grammar effect code? Same file multiple times, but only one load of the images.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Are you actually mad that I pointed out a spelling error? Most people would like their variables, etc, to be spelled correctly. Then again, most people get upset when you point out spelling errors. It's a bad habit of mine, I guess.

    At any rate, you haven't explained/showed how you're handling the list of monsters. Clearly there's nothing wrong with the code you posted. So the error lies elsewhere.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    I use a vector the problem is in that code since a data dump shows only the root paths instead of the full paths. I also get some huge negative number for my num frames. So ifstream is failing me somehow. Generally I spawn 25 monsters at a time kill them redo and eventually the loading code fails

  8. #8
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Since you want to see how I handle the list of monsters....

    Code:
    vector <Monster *>my_monsters;
    ...
    void Create_Monster()
    {
        Monster *New_Monster;
        New_Monster = new Monster("gfx/Spider/spider.txt",res_m);
        New_Monster->Set_XY(player->Get_X_Pos() + 100,player->Get_Y_Pos() + 100);
        my_monsters.push_back(New_Monster);
    }
    Nothing interesting there. It is with the values being read from the fstream not with anything else. The values are FUBAR not how I add things to my vector. If you have no clue which it is obvious you don't bugger off let someone with a few more brain cells help.

  9. #9
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Calm down charlie brown. Don't insult someone who's trying to help you about their intelligence when it's obvious since you can't figure it out, you're no more intelligent than they are. I hate kids like that.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  10. #10
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Ok so I added code to check the fstream object....

    Code:
        load_file>>temp>>D_num_frames;
        D_ANI.assign(root_path);
        D_ANI.append(temp);
    
        if(!load_file.good())allegro_message("LOAD FILE IS BAD! FLAGS SET\n EOF: %i \n BAD: %i \n FAIL: %i", load_file.eof(),load_file.bad(),load_file.fail());
    
        rm->Load_Sprite(W_ANI,W_ANI);
        rm->Load_Sprite(A_ANI,A_ANI);
        rm->Load_Sprite(D_ANI,D_ANI);
    I get the output of:

    LOAD FILE IS BAD! FLAGS SET

    EOF: 0
    BAD: 0
    FAIL: 1

    What does this mean what is happening?

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It means that, at some point, it failed to read some value. Maybe one of your data files is corrupted? For example, if it fails to parse an int, it will stop reading, set the fail flag, and not do anything else until you clear the flag. Which means that if you try to read into another variable it won't change that variable, and if that variable was uninitialized before the read attempt, it will be some random value. This is most likely the source of that "huge negative value" you're seeing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    +1 internets for using Allegro. I love that library.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  13. #13
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    The shear fact that it sets the fail bit on opening a file though? I mean I am using the same file over and over it's not like I am switching between files(not yet at least, no animations!)

  14. #14
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    @Rod, I was referring to the point of the debugger points me here the allegro.cc forums point me there, the fail bit points me there and according to this guy it is in my vector management?

  15. #15
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Problem is that there are too many open files somehow as per my question on stackoverflow.com; Oddly enough I close the file every time I am not sure why I run out of descriptors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fstream problem.
    By kevinawad in forum C++ Programming
    Replies: 9
    Last Post: 10-18-2008, 10:48 PM
  2. vector <fstream*> problem
    By mickey0 in forum C++ Programming
    Replies: 5
    Last Post: 07-18-2007, 03:58 PM
  3. problem with fstream
    By ivotron in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2006, 02:14 AM
  4. Problem with fstream
    By I BLcK I in forum C++ Programming
    Replies: 5
    Last Post: 09-30-2006, 02:28 PM
  5. Fstream Problem
    By Xterria in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2001, 11:03 PM