CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Bug in FSTREAM

  1. #1
    Join Date
    Dec 2001
    Posts
    391

    Bug in FSTREAM

    I'm creating an instance of the IFSTREAM class (ifstream myFile). I'm able to open and read from the file the object is connected to. However after closing the object and then trying to Open again I promptly get an error.

    My code looks something like this:
    ===
    ifstream inFile;

    inFile.open("e:\\Test.exe", ios::binary);
    if (!inFile)
    {cout << "File Open Error - Read" << endl; return 0;}
    .
    .
    .
    inFile.close();
    inFile.open("e:\\Test2.exe", ios::binary);
    if (!inFile)
    {cout << "File Open Error - Read2" << endl; return 0;}
    ===

    The error occurs for the second if (!inFile). Is this a bug within Visual C++'s implementation of ifstream? When i alter the ifstream object to a pointer (ie. ifstream *inFile) and apply new/delete before the next open all is fine. Why??

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    This is not a bug. closing and opening a stream does
    not clear its state. Either after closing (or before
    trying to open), you need to clear the state:


    Code:
    inFile.clear();
    See Section 13.9.1 of "The C++ Standard Library" by
    Josuttis if you have it.
    Last edited by Philip Nicoletti; January 28th, 2003 at 07:50 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured