How do you get a filename from a handle/resource?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Nielsen

    How do you get a filename from a handle/resource?

    Hi all,

    Does anyone know how you can get a filename if all you've got is the
    resource (e.g. returned by fopen())?

    The reason I ask is that I'd like to use tmpfile() to create a temporary
    file (because then it will be automatically deleted should my script
    abruptly terminate.) Once I've written my data to this file, I then
    need to pass the filename on to an external command, but I can't see any
    way to find out what the temporary file is called.

    I've tried using fstat(), but this returns everything except the filename.

    Any ideas?

    Thanks,
    Adam.
  • Janwillem Borleffs

    #2
    Re: How do you get a filename from a handle/resource?

    Adam Nielsen schreef:
    Does anyone know how you can get a filename if all you've got is the
    resource (e.g. returned by fopen())?
    >
    You can't, unless you write a wrapper which stores the file name.
    The reason I ask is that I'd like to use tmpfile() to create a temporary
    file (because then it will be automatically deleted should my script
    abruptly terminate.)
    >
    It depends on what you mean by "abruptly terminate". When your script
    crashes or manually interrupted, the temporary file won't be deleted.


    JW

    Comment

    • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

      #3
      Re: How do you get a filename from a handle/resource?

      Adam Nielsen escribió:
      Does anyone know how you can get a filename if all you've got is the
      resource (e.g. returned by fopen())?
      >
      The reason I ask is that I'd like to use tmpfile() to create a temporary
      file (because then it will be automatically deleted should my script
      abruptly terminate.) Once I've written my data to this file, I then
      need to pass the filename on to an external command, but I can't see any
      way to find out what the temporary file is called.
      >
      I've tried using fstat(), but this returns everything except the filename.
      Use tempnam() instead:




      --
      -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
      -- Mi sitio sobre programación web: http://bits.demogracia.com
      -- Mi web de humor al baño María: http://www.demogracia.com
      --

      Comment

      • Jerry Stuckle

        #4
        Re: How do you get a filename from a handle/resource?

        Adam Nielsen wrote:
        Hi all,
        >
        Does anyone know how you can get a filename if all you've got is the
        resource (e.g. returned by fopen())?
        >
        The reason I ask is that I'd like to use tmpfile() to create a temporary
        file (because then it will be automatically deleted should my script
        abruptly terminate.) Once I've written my data to this file, I then
        need to pass the filename on to an external command, but I can't see any
        way to find out what the temporary file is called.
        >
        I've tried using fstat(), but this returns everything except the filename.
        >
        Any ideas?
        >
        Thanks,
        Adam.
        >
        You can't. But then the file will be deleted when you fclose() it - and
        you should close a file you have open for writing before passing it off
        to another program, anyway.

        Rather, use tempname() to get a unique name, then open it.

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        Working...