Thread: need help with fstream

  1. #1
    gull
    Guest

    Question need help with fstream

    hi,

    i need a little help;

    i'm using an fstream to read a file multiple times:

    fin.open(testdata,ios::in);//opens fstream as fin
    if(fin.fail())
    cerr << "error opening " << testdata << endl;
    return;

    then, when the file has been read once, i ask the pointer to return to the beginning of file:

    if(fin.eof()){
    cerr << "eof reached for fin"<<endl;
    frame.MakeInvalid();//ignore
    fin.seekp(0, std::ios::beg);
    cerr<<"fin reset\n";
    }
    return 1;

    expecting to be able to re-read the file the second time:

    testinput::ReadInput(){
    double tmp;
    if(!fin.is_open()){
    cerr <<"fin has failed inside ReadInput"<<endl;
    fin.close();
    cerr <<"re-opening\n";
    cerr<<"passing "<<testdata<<" to istream:pen()\n";
    fin.open(testdata,ios::in);
    if(!fin.is_open())
    cerr<<"re-open has failed on second attempt\n";
    }

    i expect fin to still be open, but if it isn't i ask it to reopen, but it fails. can anyone explain this?
    (i am using vc++ and running the executable as a win32 console ap)

  2. #2
    Unregistered
    Guest

    sorry

    that should have read:

    i'm using an fstream to read a file multiple times:
    Code:
    fin.open(testdata,ios::in);//opens fstream as fin 
    if(fin.fail()) 
    cerr << "error opening " << testdata << endl; 
    return;
    then, when the file has been read once, i ask the pointer to return to the beginning of file:
    Code:
    if(fin.eof()){ 
    cerr << "eof reached for fin"<<endl; 
    frame.MakeInvalid();//ignore 
    fin.seekp(0, std::ios::beg); 
    cerr<<"fin reset\n"; 
    } 
    return 1;
    expecting to be able to re-read the file the second time:
    Code:
    testinput::ReadInput(){ 
    double tmp; 
    if(!fin.is_open()){ 
    cerr <<"fin has failed inside ReadInput"<<endl; 
    fin.close(); 
    cerr <<"re-opening\n"; 
    cerr<<"passing "<<testdata<<" to istream:pen()\n"; 
    fin.open(testdata,ios::in); 
    if(!fin.is_open()) 
    cerr<<"re-open has failed on second attempt\n"; 
    }
    i expect fin to still be open, but if it isn't i ask it to reopen, but it fails. can anyone explain this?
    (i am using vc++ and running the executable as a win32 console ap)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM