Re: filename check [message #22075] |
Tue, 24 October 2000 00:00 |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
Stuart Colley <src@star.ucl.ac.uk> writes:
> Can anyone suggest a way to check that a file exists before attempting to
> read from it, since the error messages obtained from trying to read from a
> non-existing file, don't make it clear that file doesn't exist.
Use the ERROR keyword to OPENR to detect an error condition quietly.
The value returned in this keyword will be non-zero if the file could
not be opened. Also the !ERROR_STATE system variable will contain
helpful information describing the error:
NAME STRING 'IDL_M_CNTOPNFIL'
MSG STRING 'OPENR: Error opening file: snorg.'
SYS_MSG STRING 'No such file or directory'
According to RSI, the error conditions in !ERROR_STATE.NAME are
guaranteed to remain constant in future versions of IDL. To implement
it in practice, consider this:
get_lun, unit
openr, unit, filename, error=err
if err NE 0 then begin
free_lun, unit
message, 'ERROR: could not open '+filename
endif
; Read file ...
I tend to use GET_LUN explicitly so that the unit is guaranteed to be
valid when it is freed. I've nit-picked about this on the newsgroup
before.
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
Re: filename check [message #22076 is a reply to message #22075] |
Tue, 24 October 2000 00:00  |
mole6e23
Messages: 31 Registered: December 1998
|
Member |
|
|
In article
<Pine.OSF.3.96.1001024143333.20360F-100000@zuaxp7.star.ucl.ac.uk>, Stuart
Colley <src@star.ucl.ac.uk> wrote:
> Can anyone suggest a way to check that a file exists before attempting to
> read from it, since the error messages obtained from trying to read from a
> non-existing file, don't make it clear that file doesn't exist.
>
> cheers,
> S
;; The following requires IDL 5.3 or higher...
;; if you don't have it, you'll have to figure
;; out the type yourself using the normal size
;; function
function file_exists, fileName
if( size( fileName, /type ) ne 7 ) then return, 0
fileList = findfile( fileName )
if( fileList[0] eq '' ) then return, 0 else return, 1
end ;; file_exists
--
Good luck!
Todd
|
|
|
Re: filename check [message #22081 is a reply to message #22075] |
Tue, 24 October 2000 00:00  |
Martin Schultz
Messages: 515 Registered: August 1997
|
Senior Member |
|
|
Stuart Colley wrote:
>
> Can anyone suggest a way to check that a file exists before attempting to
> read from it, since the error messages obtained from trying to read from a
> non-existing file, don't make it clear that file doesn't exist.
>
> cheers,
> S
No, but the error message you get when you try to open the file with
OPENR will tell you this. You may want to try out my open_file
procedure which adds a couple of additional features such as seemless
integration of the dialog_pickfile routine if you specify a name with
wildcards (or no name at all). And this does error checking...
You can find open_file.pro at
http://www.mpimet.mpg.de/~schultz.martin/idl/html/libmartin_ schultz.html
Cheers,
Martin
--
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
[[ Dr. Martin Schultz Max-Planck-Institut fuer Meteorologie [[
[[ Bundesstr. 55, 20146 Hamburg [[
[[ phone: +49 40 41173-308 [[
[[ fax: +49 40 41173-298 [[
[[ martin.schultz@dkrz.de [[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
|
|
|
Re: filename check [message #22083 is a reply to message #22075] |
Tue, 24 October 2000 00:00  |
Alex Schuster
Messages: 124 Registered: February 1997
|
Senior Member |
|
|
Stuart Colley wrote:
> Can anyone suggest a way to check that a file exists before attempting to
> read from it, since the error messages obtained from trying to read from a
> non-existing file, don't make it clear that file doesn't exist.
You can check this with the FINDFILE() function.
Alex
--
Alex Schuster Wonko@weird.cologne.de PGP Key available
alex@pet.mpin-koeln.mpg.de
|
|
|