Ask a Question related to UNIX Programming, Design and Development.
-
seemanta dutta #1
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
-
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... -
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... -
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... -
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: ... -
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... -
Gianni Mariani #2
Re: how to get filename from a FILE *fp?
seemanta dutta wrote:
NO.> 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);
Gianni Mariani Guest
-
scriptOmatic #3
Re: how to get filename from a FILE *fp?
seemanta dutta wrote:
No,>
> 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);
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
-
Juha Laiho #4
Re: how to get filename from a FILE *fp?
Gianni Mariani <gi2nospam@mariani.ws> said:
.... but in any case, it may be that the file with the name that was>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.
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
-
Gianni Mariani #5
Re: how to get filename from a FILE *fp?
Juha Laiho wrote:
The example that I described for linux will give you the ACTUAL open> 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.
file. Sometimes it's not a filename at all, it's somthing like
socket:[1234].
Gianni Mariani Guest
-
Juha Laiho #6
Re: how to get filename from a FILE *fp?
Gianni Mariani <gi2nospam@mariani.ws> said:
The proc entry is a symlink, so it might point to a non-existent file,>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].
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



Reply With Quote

