ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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;
}
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.