Get full path of file

Nov 26, 2013 at 9:35am
Hi,
I wanted to make my program read the file "input.txt". I did it successfully, but now I want to get the full path of the file "input.txt". Is there any way to do it?

My output should be :
"C:\Documents and Settings\Administrator\Desktop\myfile\debug\input.txt"


And here's my code so far :
1
2
3
4
5
6
7
8
9
10
    FILE *file;
    std::string fullFileName;

    file = fopen("input.txt", "rb");

    if(file != NULL)
    {
       // getfullfilename(file, fullFileName);
       // std::cout << fullFileName.c_str() << std::endl;
    }


Last edited on Nov 26, 2013 at 9:39am
Nov 26, 2013 at 10:37am
you can specify the full path of the input file w/o doing any special thing :

1
2
3
4
5
6
7
FILE* file;
std::string fileName( "C:\\Documents and Settings\\Administrator\\Desktop\\myfile\\debug\\input.txt" );

file = fopen( fileName.c_str(), "rb" );

if( file != NULL )
    std::cout << fileName << std::endl;
Last edited on Nov 26, 2013 at 10:39am
Nov 26, 2013 at 10:51am
@shadow fiend, I don't think he wants that, but i could be wrong?
he wants something to return him the path of where the file is sitting in the file system.

i think there is stuff in boost to do this, and some windows specific functions too.

OP, you'd have to call this before your call to open. on line 4.

and also be aware you could have lots of files called 'input.txt' on your system.

just found this:
http://www.cplusplus.com/forum/general/3221/
Last edited on Nov 26, 2013 at 10:55am
Nov 26, 2013 at 10:55am
If he's using a framework and a GUI then the class that handles the open file dialog will return the full path to the file selected. Would be handy if we knew whether he was in the terminal or a GUI environment, which OS and what framework he is using...
Nov 26, 2013 at 12:11pm
The file spec is relative to the current directory.

You get the current directory with GetCurrentDirectory
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364934%28v=vs.85%29.aspx

or getcwd() on POSIX
http://pubs.opengroup.org/onlinepubs/9699919799/functions/getcwd.html

You then append your file specification to that.
Nov 27, 2013 at 12:21am
On Windows, instead of acquiring the current/working directory and then building the path yourself, you could use the WinAPI call GetFullPathName()

GetFullPathName function
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364963%28v=vs.85%29.aspx

or the corresponding Microsoft CRT function, _fullpath

_fullpath, _wfullpath
http://msdn.microsoft.com/en-us/library/506720ff.aspx

POSIX has a related function, realpath, but I believe this fails if the file or directory does not already exist, unlike the Windows calls.

realpath
http://pubs.opengroup.org/onlinepubs/009695399/functions/realpath.html

Boost.Filesystem provides the canonical function which behaves like realpath (i.e. the path must be for a file or directory which exists.)

Filesystem Library / canonical
http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#canonical

Andy

PS The Microsoft CRT also provides _getcwd(), which has the same signature as the POSIX getcwd() call.

_getcwd, _wgetcwd
http://msdn.microsoft.com/en-us/library/sf98bd4y.aspx
Last edited on Nov 27, 2013 at 12:36am
Topic archived. No new replies allowed.