[vtkusers] BUG: vtkDataReader::OpenVTKFile() ... + solution

Schaap, J.A. (LKEB) J.A.Schaap at lumc.nl
Thu Jul 18 08:10:32 EDT 2002


Hi,

According to the documentation one can use the function vtkDataReader::OpenVTKFile() to check if a file is valid. 
If you try to open a non-exsiting file, it returns 1.

This is because of the line:

    this->IS = new ifstream(this->FileName, ios::in);


which should be replaced by:

    this->IS = new ifstream(this->FileName, ios::in | ios::nocreate);

Best regards, Jorrit Schaap.





Below you find the current code of the implementation in version 4.0.

int vtkDataReader::OpenVTKFile()
{
  if (this->ReadFromInputString)
    {
    if (this->InputString)
      {
      vtkDebugMacro(<< "Reading from InputString");
      this->IS = new istrstream(this->InputString, this->InputStringLength);
      return 1;
      }
    }
  else
    {
    vtkDebugMacro(<< "Opening vtk file");

    if ( !this->FileName || (strlen(this->FileName) == 0))
      {
      vtkErrorMacro(<< "No file specified!");
      return 0;
      }
    this->IS = new ifstream(this->FileName, ios::in);
    if (this->IS->fail())
      {
      vtkErrorMacro(<< "Unable to open file: "<< this->FileName);
      delete this->IS;
      this->IS = NULL;
      return 0;
      }
    return 1;
    }

  return 0;
}





More information about the vtkusers mailing list