how to get filename from a FILE *fp?

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default how to get filename from a FILE *fp?

    hi,
    my question is, is there a way to know the filename of a file from its
    high level 'FILE' pointer after I have 'fopened' it?

    like
    char *fname;
    FILE *fp;
    fp=fopen("foofile","r");
    fname=returnfilename(fp);
    seemanta dutta Guest

  2. Similar Questions and Discussions

    1. How can I access the filename of current file as atemplate parameter?
      How can I access the filename of current file as a template parameter? Hi I want to use a template expression or parameter to simply display the...
    2. Move file to a new filename every month to a different name
      I have written only very simple batch scripts previously, and I'm stumped when trying to move a file on Windows 2000 to a different file name each...
    3. Load variables from text then loading image file with the filename...
      Load variables from text file then loading image file with the filename given in text file The problem is, that i need flash to load variable from...
    4. Adding filename to downloaded file
      Hi, I'm using php to generate a csv file and want to force the user to download it to their local PC. Trouble is, when I use: ...
    5. Place part of filename into the file
      Warning: rank newbie here. I have a series of ascii files, like file0037.txt file0038.txt file0039.txt etc. I have a statement somewhere within...
  3. #2

    Default Re: how to get filename from a FILE *fp?

    seemanta dutta wrote:
    > hi,
    > my question is, is there a way to know the filename of a file from its
    > high level 'FILE' pointer after I have 'fopened' it?
    >
    > like
    > char *fname;
    > FILE *fp;
    > fp=fopen("foofile","r");
    > fname=returnfilename(fp);
    NO.

    Gianni Mariani Guest

  4. #3

    Default Re: how to get filename from a FILE *fp?

    seemanta dutta wrote:
    >
    > hi,
    > my question is, is there a way to know the filename of a file from its
    > high level 'FILE' pointer after I have 'fopened' it?
    >
    > like
    > char *fname;
    > FILE *fp;
    > fp=fopen("foofile","r");
    > fname=returnfilename(fp);
    No,
    But, if you want to look at it,
    there is source code in the OSM project called
    CCfopen/CCfclose that you can adapt to do the above.
    Hope this helps.

    --
    [url]http://ftp.opensysmon.com[/url] is a shell script archive site with an
    open source system monitoring and network monitoring software package.
    Many platforms are supplied already compiled.
    scriptOmatic Guest

  5. #4

    Default Re: how to get filename from a FILE *fp?

    Gianni Mariani <gi2nospam@mariani.ws> said:
    >seemanta dutta wrote:
    >> hi,
    >> my question is, is there a way to know the filename of a file from its
    >> high level 'FILE' pointer after I have 'fopened' it?
    >>
    >> like
    >> char *fname;
    >> FILE *fp;
    >> fp=fopen("foofile","r");
    >> fname=returnfilename(fp);
    >
    >I lied.
    >
    >The answer is it depends on the system.
    .... but in any case, it may be that the file with the name that was
    used at the time of fopen() does not exist, or the name might point
    to a different file. So even if you can find the name, it's not wise
    to trust it.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho Guest

  6. #5

    Default Re: how to get filename from a FILE *fp?

    Juha Laiho wrote:
    > Gianni Mariani <gi2nospam@mariani.ws> said:
    >
    >>seemanta dutta wrote:
    >>
    >>>hi,
    >>>my question is, is there a way to know the filename of a file from its
    >>>high level 'FILE' pointer after I have 'fopened' it?
    >>>
    >>>like
    >>> char *fname;
    >>> FILE *fp;
    >>> fp=fopen("foofile","r");
    >>> fname=returnfilename(fp);
    >>
    >>I lied.
    >>
    >>The answer is it depends on the system.
    >
    >
    > ... but in any case, it may be that the file with the name that was
    > used at the time of fopen() does not exist, or the name might point
    > to a different file. So even if you can find the name, it's not wise
    > to trust it.
    The example that I described for linux will give you the ACTUAL open
    file. Sometimes it's not a filename at all, it's somthing like
    socket:[1234].

    Gianni Mariani Guest

  7. #6

    Default Re: how to get filename from a FILE *fp?

    Gianni Mariani <gi2nospam@mariani.ws> said:
    >Juha Laiho wrote:
    >> Gianni Mariani <gi2nospam@mariani.ws> said:
    >>
    >>>seemanta dutta wrote:
    >>>>my question is, is there a way to know the filename of a file from its
    >>>>high level 'FILE' pointer after I have 'fopened' it?
    >>>
    >>>The answer is it depends on the system.
    >>
    >> ... but in any case, it may be that the file with the name that was
    >> used at the time of fopen() does not exist, or the name might point
    >> to a different file. So even if you can find the name, it's not wise
    >> to trust it.
    >
    >The example that I described for linux will give you the ACTUAL open
    >file. Sometimes it's not a filename at all, it's somthing like
    >socket:[1234].
    The proc entry is a symlink, so it might point to a non-existent file,
    or to some other file than what was opened. Ok, linux appears to be
    smart here that it adds " (deleted)" to the proc entry link path, so
    that you can't open it without understanding what you're doing. And
    strangely, even though the entry claims to be a symlink, it actually
    behaves in file-like manner in that that you can manipulate it even
    after the original file is removed. But still as I said, you can't
    trust the actual _name_ contained in the proc entry link path (so,
    you can't count on being able to reopen the same file by name).


    A shell script to demonstrate the situation:

    #! /bin/bash
    echo data > /tmp/foodata
    echo "First ls"
    ls -l /proc/$$/fd /tmp/foodata 2>&1
    echo "Opening file"
    exec < /tmp/foodata
    echo "Removing file"
    rm /tmp/foodata
    sleep 1
    echo "Recreating file"
    echo otherdata > /tmp/foodata
    echo "Second ls"
    ls -l /proc/$$/fd /tmp/foodata 2>&1
    sleep 1
    echo "Reading previously opened file"
    while read data; do
    echo "Read: $data"
    done
    echo "Catting symlink"
    cat /proc/$$/fd/0
    echo "Catting pointed-to file"
    cat "$(readlink /proc/$$/fd/0)"
    #End


    And output of the above:
    First ls
    -rw-r--r-- 1 jlaiho jlaiho 5 Jul 18 14:03 /tmp/foodata

    /proc/29015/fd:
    total 0
    lrwx------ 1 jlaiho jlaiho 64 Jul 18 14:03 0 -> /dev/tty3
    l-wx------ 1 jlaiho jlaiho 64 Jul 18 14:03 1 -> /home/jlaiho/test.out
    lrwx------ 1 jlaiho jlaiho 64 Jul 18 14:03 2 -> /dev/tty3
    lrwx------ 1 jlaiho jlaiho 64 Jul 18 14:03 255 -> /dev/tty3
    lr-x------ 1 jlaiho jlaiho 64 Jul 18 14:03 3 -> /home/jlaiho/foo.sh
    Opening file
    Removing file
    Recreating file
    Second ls
    -rw-r--r-- 1 jlaiho jlaiho 10 Jul 18 14:03 /tmp/foodata

    /proc/29015/fd:
    total 0
    lr-x------ 1 jlaiho jlaiho 64 Jul 18 14:03 0 -> /tmp/foodata (deleted)
    l-wx------ 1 jlaiho jlaiho 64 Jul 18 14:03 1 -> /home/jlaiho/test.out
    lrwx------ 1 jlaiho jlaiho 64 Jul 18 14:03 2 -> /dev/tty3
    lrwx------ 1 jlaiho jlaiho 64 Jul 18 14:03 255 -> /dev/tty3
    lr-x------ 1 jlaiho jlaiho 64 Jul 18 14:03 3 -> /home/jlaiho/foo.sh
    Reading previously opened file
    Read: data
    Catting symlink
    data
    Catting pointed-to file
    cat: /tmp/foodata (deleted): No such file or directory

    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
    Juha Laiho Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139