LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2004, 02:53 AM   #1
barisdemiray
Member
 
Registered: Sep 2003
Location: Ankara/Turkey
Distribution: Slackware
Posts: 155

Rep: Reputation: 30
Getting file path from FILE pointer in C


Hello,
i've quickly searched the archives but found nothing. Do you know a way of getting file name from FILE pointer? I'll use tmpfile() but it does return file pointer only (i know i can use tmpnam() and open the file at the returned path but i'm wondering whether there is a way of doing this). Thanks.
 
Old 10-21-2004, 05:45 AM   #2
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
Hi barisdemiray!
Well from my concepts of what filing is, i don't thing that there is a simple way to get the file path from a file pointer, coz the file pointer or stack has no information about the physical path of the file, so you better use the way you use the way you suggested yourself.
Tayyab
 
Old 10-21-2004, 08:54 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: Getting file path from FILE pointer in C

Quote:
Originally posted by barisdemiray
Do you know a way of getting file name from FILE pointer? I'll use tmpfile() but it does return file pointer only
This is possible, but there two things to keep in mind:
  • When using tmpfile(), you get an open FILE* to the file, but it also gets unlinked (deleted) by tmpfile() immediately after opening it. So while your process has it still open and it can read and write the file as long as it does not fclose() it, you will not see it with "ls -la /tmp". When your process closes the file, it's gone automatically (see "man tmpfile"). Maybe this is why you are asking this in the first place, because you don't see any file in /tmp?
  • The only possible way (i know of) to get the full path is sort of a workaround. So it's probably better to use tempnam() or tmpnam().
  • Geting the file's path name is probably not of any use, since it is already gone.... (except for the program's process itself, which has no need for the name, as it already has the file open)

Anyways, here's how it can be done:
(note the "(deleted)" in the output of this program)
Code:
#include <stdio.h>
#include <unistd.h>

#define MAXLEN 200

int main(int argc, char **argv)
{
    FILE *file;
    int fd;
    char procpath[MAXLEN + 1];
    char filepath[MAXLEN + 1];

    /*
     * Open a temp-file 
     */
    file = tmpfile();
    if (file == NULL) {
        perror(*argv);
        return 1;
    }

    /*
     * Get the low-level file descriptor of the open file 
     */
    fd = fileno(file);
    if (fd < 0) {
        perror(*argv);
        fclose(file);
        return 1;
    }

    /*
     * Construct a string with the /proc path of the file
     * descriptor (which is a symbolic link to the real
     * file).
     */
    snprintf(procpath, MAXLEN, "/proc/self/fd/%d", fd);

    /*
     * Get the path the symlink is pointing to.
     */
    if (readlink(procpath, filepath, (size_t) MAXLEN) < 0) {
        perror(*argv);
        fclose(file);
        return 1;
    }

    /* Output the full path of the temp-file opened by tmpfile() */
    printf("The temp file is: %s\n", filepath);

    return 0;
}
 
Old 10-22-2004, 12:58 AM   #4
barisdemiray
Member
 
Registered: Sep 2003
Location: Ankara/Turkey
Distribution: Slackware
Posts: 155

Original Poster
Rep: Reputation: 30
Re: Re: Getting file path from FILE pointer in C

Thanks Hko and cppkid! I've written my own tmpfile() function that returns a file name and it uses the outputs of time(NULL), getpid(), random() (but still it worths to read Hko's code and learn something more ;-)) as this:


Code:
/**
 * Returns a random file name
 * @return A temporary file name
 */
string Config::getTempFileName()
{
        FILE *tmpFile;
        stringstream tmpFilePath;

create_again:
        srand(time(NULL));
        tmpFilePath.str("");
        tmpFilePath  << "/tmp/lbr-lb-" << random() << "-" << random()
                     << "-" << getpid() << "-" << time(NULL)%1000;

        /*
         * check the file's existency
         */
        if ( (tmpFile = fopen(tmpFilePath.str().c_str(), "r")) )        {
                fclose(tmpFile);
                goto create_again;
        }

        return tmpFilePath.str();
}
But, there would be a race condition between the time of check in this file (file exists or not) and the time of opening the file at the caller. May be returning a file pointer and placing the file name in a formal parameter may help. Anyway, thanks guys!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the absolute path of file using file descriptor. appas Programming 7 01-19-2012 11:47 AM
PATH file location corbis_demon Linux - Newbie 1 10-08-2004 05:34 AM
problem with extern file pointer in library linuxping Programming 5 02-08-2004 09:55 PM
connect file to path gyue Linux - General 2 11-06-2003 06:27 AM
PATH variable file pongsu Linux - General 2 09-25-2003 04:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration