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

Andy Cedilnik andy.cedilnik at kitware.com
Thu Jul 18 09:51:38 EDT 2002


Hello,

The way we commonly do this in VTK is using stat. For example in
vtkDataReader.cxx: 

    if (stat(this->FileName, &fs) != 0) 
      {
      vtkErrorMacro(<< "Unable to open file: "<< this->FileName);
      this->SetErrorCode( vtkErrorCode::CannotOpenFileError );
      return 0;
      }
    this->IS = new ifstream(this->FileName, ios::in);

This is in the CVS (4.1.1) version of VTK.
So, things are fixed, but you will have to either get the nightly
version of VTK or wait for next release.

				Andy
    

On Thu, 2002-07-18 at 08:10, Schaap, J.A. (LKEB) wrote:
> 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