Is there a way to find out the filename associated with a specific FILE * pointer? (OS: Red Hat EL 5)
I'm running into a strange bug where we verify that a file exists and then about two or three lines later, when we try to rename the file, we get an error that says there is no such file.
The code is something like the following...
...
FILE *fp;
char temp_filename[MAX_NAME_LENGTH];
char perm_filename[MAX_NAME_LENGTH];
...
fp = fopen(temp_filename, "w");
...
// done writing to temp file, move it to permanent file
if(fp)
{
fclose(fp);
if (rename(temp_filename, perm_filename))
{
cout << "Error: can't rename file: " << strerror(errno) << endl;
}
}
The strerror command shows "No such file or directory," which is a head-scratcher because fp is open and we are in the if block of code to begin with.
I'm trying to find out if the file has been moved or deleted out from under me, i.e. if temp_filename is still the filename associated with fp. For debugging, I've tried to do a stat on temp_filename and an fstat on fp, and then cout the inode numbers (st_ino) to see if they are the same, but I can't get it to compile since the first argument to fstat(int, struct stat *) needs to be an int, not a FILE pointer.
The target operating system, if it matters, is RHEL 5, x86_64.
Help, please.
posted by bfranklin at 8:42 AM on April 12, 2012