Getting a full path by using a text file name

How to get a full path of a directory only using a text file name? (Btw i am using visual studio not windows)
What do you want to happen if there are multiple text files with the same name in different directories?
the text file will be special like "8 file.txt".
What if there is "C:/hello/8 file.txt" and "C:/goodbye/8 file.txt"?
Would make sure that there are no repetition.
Your question is a little unclear.

If you just provide the file name, the file will be assumed to be in the current working directory. This can be obtained using the POSIX call getcwd, which turns up as _getcwd in the Microsoft CRT. There's also the WinAPI GetCurrentDirectory function which does the same job.

(On Windows you could use GetFullPathName to obtain the full path for the file, and then chop off the file name. But that would just be more work to obtain the same path as before.)

There is no automatic way of working out where a file is from its name.

You could search for the file, if you know it has a unique name. But that would be slow unless you restricted the search to a small subtree.

Andy
Last edited on
How about in the same directory?
What do you mean by same? Same as what?

If you cd to the folder where the exe is (e.g. c:\test) and run it there (myapp.exe), then the current directory will be the same as the exe (c:\test). But if you cd to (e.g.) c:\test\data and then run the app like ..\myapp.exe, then the working directory will be c:\test\data, not the app folder (c:\test).

You could obtain the directory where the app is located and then set the working directory to the same value. Is this what you mean? But note that it is not best practice to store data in the same folder as the exe on Windows or Linux.

Andy
Last edited on
With boost filesystem library http://www.boost.org/doc/libs/1_55_0/libs/filesystem/doc/index.htm :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <boost/filesystem.hpp>
#include <iostream>

void info( const boost::filesystem::path& relative_path )
{
    using namespace boost::filesystem ;

    std::cout << "rel path: " << relative_path << '\n' ;
    std::cout << "cwd: " << current_path() << '\n' ; // current working directory

    const path absolute_path = absolute(relative_path) ;
    std::cout << "absolute: " << absolute_path << '\n'
               << "root dir: " << absolute_path.root_directory() << '\n'
               << "root path: " << absolute_path.root_path() << '\n'
               << "parent dir: " << absolute_path.parent_path() << '\n'
               << "file name: " << absolute_path.filename() << '\n'
               << "-----------------------------------------------\n" ;
}

int main()
{

    info( "main.cpp" ) ; // relative path

    boost::filesystem::path data_dir = "data/out/text" ;
    boost::filesystem::create_directories(data_dir) ;

    info( data_dir / "result.txt" ) ; // relative path

    boost::filesystem::current_path(data_dir) ; // change cwd
    info( "result.txt" ) ; // relative path
}

http://coliru.stacked-crooked.com/a/b6cc0b543adc9439

With Microsft C++, the header <filesystem> has similar functionality
http://msdn.microsoft.com/en-us/library/hh874694.aspx
thanks
Topic archived. No new replies allowed.