fstream problem

This is a discussion on fstream problem within the C++ Programming forums, part of the General Programming Boards category; Oddly enough I close the file every time I am not sure why I run out of descriptors. The constructor ...

  1. #16
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,165
    Oddly enough I close the file every time I am not sure why I run out of descriptors.
    The constructor for `std::ifstream' can fail for reasons other than exhausted file descriptors, but if you have exhausted them somehow the problem is not in the code you posted as oogabooga explained.

    If I were you, I'd take a good long look at how `Load_Sprite' works.

    Soma

  2. #17
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    There is a timeout on the file descriptors - so I have been told on stack overflow. the files don't immediately close. What is that time out time?

  3. #18
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    BTW it would crash before ever reading anything in from the file meaning it never reached Load_Sprite. I test all my code in a hard call from main when I add something specific(unless it requires multiple frames to occur)

  4. #19
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try running something like this:
    Code:
    #include <iostream>
    #include <fstream>
    
    int f() {
        std::ifstream ifs("file.txt");
        int n;
        ifs >> n;
        ifs.close();
        return n;
    }
    
    int main() {
        for (int i = 0; i < 1000; i++)
            std::cout << f();
        return 0;
    }
    It works for me. With a file that simply has the number 1 in it, I get 1000 1's.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #20
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319
    Load_sprite only parses the sprites in a folder in and places them in a vector to be grabbed by the monster class later.

    fstream is implemented differently in different compilers(so I hear) what I was told is with more processes come more available handles to access files. Oddly enough when I added a data manger class to the resource manager class and parsed everything in there and never reloaded the file after that it works perfectly and probably uses slightly less memory(unconfirmed).

    Code:
    int     Sprite::Load_Sprite(string loadfile, string id)
    {
        BITMAP *temp;       //picture temp file
        BITMAP *atemp;      //alpha temp file
        vector <BITMAP *> t;//temp vector to pushback after loading
        string lf;          //loadfile string to contain real paths
        string cnt;         //count string for the 001 002 003 etc.
        stringstream ss;    //to format the cnt string.
    
        int count = 1;
        loading<<"LOADING SPRITE: "<<id.c_str()<<" FROM "<<loadfile.c_str()<<endl;
        drawing_mode(DRAW_MODE_TRANS, NULL, 0, 0);
        set_write_alpha_blender();
        do
        {
            ss.width(3);
            ss.fill('0');
            ss << count;
            cnt = ss.str();
    
            lf.assign(loadfile);
            lf.append(cnt);
            lf.append(".bmp");
    
            temp = load_bitmap(lf.c_str(),NULL);
            if(!temp)break;     //if we have loaded all the frames break out of the loading loop.
            loading<<"...LOADED PORTION: "<<lf.c_str()<<endl;
    
            lf.assign(loadfile);
            lf.append(cnt);
            lf.append("a.bmp");
    
            atemp = load_bitmap(lf.c_str(),NULL);
            draw_trans_sprite(temp, atemp, 0, 0);
            destroy_bitmap(atemp);
    
            if(temp)t.push_back(temp);
            ss.str("");
            count++;
    
        }while(temp);
        loading<<endl<<endl;
        drawing_mode(DRAW_MODE_SOLID,NULL,0,0);
        Sprites.push_back(t);
        identifiers.push_back(id);
        return t.size();
    }

Page 2 of 2 FirstFirst 12
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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21