getting a filepath string from a FILE* ??

Started by
5 comments, last by datadawgx 19 years, 3 months ago
is there a way to aquire a file path (like, "C:/MYFOLDER/MYFILE.EXT") from a FILE* to a fopened stream? someone told me i could, but i'm not having much luck finding where.
Advertisement
#include <iostream>#include <string>#include <conio.h>using namespace std;#define NUL '\0'std::string FilePath(std::string file){	int i = file.length();	for(i; i>=0; i--){		if(file =='\\'){			return file.substr(0,(i+1));			break;		}	}	return "";}int main(int argc, char **argv) {	std::string path;	std::string filename;	filename = "c:\\tmp\\tmp.txt";	path = FilePath(filename);	cout << "file name = " << filename << endl;	cout << "path = " << path.c_str() << endl;	cout << "app path = " << FilePath(*argv) << endl;	return 0;}//end main()
Well if you are using C++ this code i have just written may help...

win_file.h
#ifndef _ENHANCED_FILE_#define _ENHANCED_FILE_const unsigned int FILE_PATH_SIZE			= 400;const unsigned int FILE_NAME_SIZE			= 200;const unsigned int FILE_EXT_SIZE			= 5;const unsigned int FILE_ACCESS_MODE_SIZE	= 5;#include <stdio.h>#include <string.h>#include "win_err_def.h"using namespace COM_DAVESDUMP;class file{public:	file();	file(char* filePath, char* accessMode);	virtual ~file();	void	AssignFile(char* filePath, char* accessMode);	FILE*	GetFilePointer();	bool	fileOpen;private:	void OpenFile();	void CloseFile();	void GetFileNameFromPath();	void GetExtensionFromPath();	void DetermineFileSize();	FILE*	fp;	bool			fileSet;	unsigned long	fileSize;	char			filePath[FILE_PATH_SIZE];	char			fileName[FILE_NAME_SIZE];	char			fileExt[FILE_EXT_SIZE];	char			fileAccessMode[FILE_ACCESS_MODE_SIZE];};#endif //_file_


win_file.cpp
#include "win_file.h"#include "win_err.h"extern COM_DAVESDUMP::win_err* ErrorHandler;////////////////////////////////////////////////////////////////////////////////////////////	Name:	file()//	Params:	filePath	- pointer to the incoming path//			accessMode	- pointer to the incoming access mode//	Desc:	Contructor for the file class. Checks the validity of the incoming strings,//			sets the local file data tot eh incoming values and calls the open file func.//////////////////////////////////////////////////////////////////////////////////////////file::file(char* filePath, char* accessMode){	AssignFile(filePath, accessMode);}////////////////////////////////////////////////////////////////////////////////////////////	Name:	file()//	Params:	N/A//	Desc:	Contructor for the file class. //////////////////////////////////////////////////////////////////////////////////////////file::file(){}////////////////////////////////////////////////////////////////////////////////////////////	Name:	~file()//	Params:	N/A//	Desc:	Destructor for the file class. Calls the CloseFile() function when the //			any objects of this class go out of scope.//////////////////////////////////////////////////////////////////////////////////////////file ::~file(){	CloseFile();}////////////////////////////////////////////////////////////////////////////////////////////	Name:	OpenFile()//	Params:	N/A//	Desc:	After checking whether this file has already been opened, opens if it hasn't//			been opened and sets the value of fileOpen accordingly.//////////////////////////////////////////////////////////////////////////////////////////void file::OpenFile(){	if (fileOpen == false)	{		try		{			fp = fopen(&filePath[0], &fileAccessMode[0]);			if (fp == NULL) throw "Error Opening File!";			win_err_def err("Error Opening File!", 0);		}		catch(...)		{			ErrorHandler->Exception();		}		if (fp != NULL)		{			fileOpen = true;			GetFileNameFromPath();			GetExtensionFromPath();			DetermineFileSize();		}	}}////////////////////////////////////////////////////////////////////////////////////////////	Name:	GetFilePointer()//	Params:	N/A//	Desc:	This function returns pointer returns the member file pointer to the calling//			class only if it is valid, otherwise NULL is returned.//////////////////////////////////////////////////////////////////////////////////////////FILE* file::GetFilePointer(){	if (fileOpen == true)	{		return fp;	}	return NULL;}////////////////////////////////////////////////////////////////////////////////////////////	Name:	CloseFile()//	Params:	N/A//	Desc:	This function closes the file pointed to by the member file pointer only if//			the file has been opened.//////////////////////////////////////////////////////////////////////////////////////////void file::CloseFile(){	if (fileOpen == true)	{		fclose(fp);		if (fp == NULL)		{			fileOpen = true;		}	}}////////////////////////////////////////////////////////////////////////////////////////////	Name:	GetFileNameFromPath()//	Params:	N/A//	Desc:	This function is used to extract the file name//////////////////////////////////////////////////////////////////////////////////////////void file::GetFileNameFromPath(){	unsigned long	lengthOfPath	=	0;	unsigned long	pathPosPtr		=	0;	lengthOfPath = strlen(&filePath[0]);	for (	pathPosPtr = (lengthOfPath - 1); 			pathPosPtr >= 0; 			pathPosPtr--)	{		if ( filePath[pathPosPtr] == '\\')		{			strcat(&fileName[0], &filePath[pathPosPtr+1]);			break;		}	}}////////////////////////////////////////////////////////////////////////////////////////////	Name:	GetExtensionFromPath()//	Params:	N/A//	Desc:	This function is used to extract the file extension//////////////////////////////////////////////////////////////////////////////////////////void file::GetExtensionFromPath(){	unsigned long	lengthOfPath	=	0;	unsigned long	pathPosPtr		=	0;	lengthOfPath = strlen(&filePath[0]);	fileExt[0] = NULL;	for (	pathPosPtr = (lengthOfPath - 1); 			pathPosPtr >= 0; 			pathPosPtr--)	{		if ( filePath[pathPosPtr] == '.')		{			strcat(&fileExt[0], &filePath[pathPosPtr+1]);			break;		}	}}////////////////////////////////////////////////////////////////////////////////////////////	Name:	DetermineFileSize()//	Params:	N/A//	Desc:	This function is used to get the file size of the file.//////////////////////////////////////////////////////////////////////////////////////////void file::DetermineFileSize(){	fseek(fp, 0, SEEK_END);	fileSize = ftell(fp);}////////////////////////////////////////////////////////////////////////////////////////////	Name:	AssignFile()//	Params:	filePath	- pointer to the incoming path//			accessMode	- pointer to the incoming access mode//	Desc:	This file sets the object a file.//////////////////////////////////////////////////////////////////////////////////////////void file::AssignFile(char* filePath, char* accessMode){	if ( fileSet == false )	{		if ( (filePath != NULL) && (accessMode != NULL) )		{			this->fileOpen = false;			strcpy(&this->filePath[0], filePath);			strcpy(&this->fileAccessMode[0], accessMode);			OpenFile();			fileSet = true;		}	}}


It's a simple wrapper for the FILE*,

Basically u assigned through the constructor the path and open option like you would use for fopen, but this wrapper also does things like remembering the path, determining the size of the file and also separates out the file name from the path etc, also the extension

You could add one simple 'get' function to return teh path the file is associated with in order to get what you want.

To answer u exact question,

I dont know of a way of retrieveing the path from the file pointer, but there most likely is a way, you could try accessing what the pointer is pointing to.

ace
i don't have the luxury of just creating a new class. if i wanted to wrap around the FILE* and pass a char* to the path along with it, i would! but i'm trying not to modify existing library structures by doing anything of the sort.

good examples though, ty. im more wondering about ways of getting a path from the existing FILE structure.
No I dont think you can, because the FILE structure only holds offsets and flags regarding where the OS places/reads the file from disk. (ie. A FILE struct is a array of IO blocks representing a single file, the size of this file is set by the OS, max number of open files).

Some of the C++ standard libs such as ifstream and/or ofstream may have something to do this, but the C standard libs definitely doesn't.
i don't know if there is a way to get a file name from a FILE *. the problem is that, on some systems the FILE * may not actually point to a file, or may point to a file that has been deleted, etc.
that's what i was afraid of. thx guys

This topic is closed to new replies.

Advertisement