Thread: vector <fstream*> problem

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    32

    vector <fstream*> problem

    hi, I can't understand how use it:
    Code:
    		
    string temp_str;
    vector <fstream*> file;
    for (int i=0; i < 2; i++) {
       fstream* ff = new fstream;
       *ff << i.toString(); //pseudo-code
        file.push_back(ff);
         ff->close();
    }
    // now the files are filled with two different strings  ---- MY problem begin here...
    vector <fstream*>::iterator fIter; int i;
    for (fIter = file.begin(), i=0; fIter != file.end(); ++fIter, i++) {
             (*fIter)->open(computeFileName(i).c_str(), ios_base::in); 
             *(fIter[i]) >> temp_str;
              //*(fIter[0]) >> temp_str; //with this, it works! why?
    }
    I have 3 files to open and read. In temp_str goes the right string only when i=0; other times when I=1,2 temp_str take nothing. It works all with *(fIter[0]) >> temp_str;
    Anyone could explain me how understand this vector??
    thanks
    Last edited by mickey0; 07-18-2007 at 06:00 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    because you open the (*fIter) stream, not fIter[i] (which can not exist when the Iterator points to the last item in the vector...

    Anyway, if you open file on each iteration, why you need a vector of pointers to streams instead of one temp var of type fstream local for the loop?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    32
    hi, I don't understand what your last question mean. could you post your code? thanks

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You don't need a vector of fstreams at all. If you close the stream each time, then just use a single fstream variable.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    32
    sorry the code is again incomplete...I can have n files (eg 20, 30) and I need to open they 2 at time (then I close two files and go on with other two). I think it's right. probabily I can't keep open many files together.........thanks

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The point is that you have a block of code here:
    Code:
    {
       fstream* ff = new fstream;
       *ff << i.toString(); //pseudo-code
        file.push_back(ff);
         ff->close();
    }
    In that block of code, you use the fstream and then close it. Once you've closed the fstream, there is no reason to keep the variable around:
    Code:
    {
       fstream ff;
    
       // open file and process
    
       // ff closes automatically
    }
    Then later, just loop over i:
    Code:
    for (int i = 0; i < 2; ++i)
    {
       fstream ff;
       ff.open(computeFileName(i).c_str(), ios_base::in);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM