5

I am trying to return the path of the cpp file I am running. Does anyone know a method or a way to implement this? For example lets say I have this file test.cpp at the path in my computer "C:\Programming\Visual Studio\Test\Test\test.cpp".

Is there a way to get this path without manually typing it? I am trying to determine a method of using c++ to return this path.

For my ftp program I need to get the list of .txt, .pdf, .etc files, which are located at the same path as the .cpp file. This is why I want the .cpp path and not the .exe path.

Any suggestions?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
VMA92
  • 469
  • 2
  • 8
  • 19
  • 2
    Seems like an X/Y problem, why do you need this? – Borgleader Sep 30 '14 at 17:56
  • 1
    Use the `__FILE__` macro – drescherjm Sep 30 '14 at 17:59
  • @Borgleader I am trying to build a client / server ftp program. I need to list the files both from the server and client. If I request it from a different computer it may need a different path which is why I was trying to determine a way to do this without manually putting the path. – VMA92 Sep 30 '14 at 17:59
  • So you need the path of the executable not the source code file? – Borgleader Sep 30 '14 at 18:01
  • 1
    Check [`boost::path`](http://www.boost.org/doc/libs/1_56_0/libs/filesystem/doc/index.htm) for dealing with file and path names portably. – πάντα ῥεῖ Sep 30 '14 at 18:02
  • @drescherjm this returned me just the file name. Is there a way to get the full path? – VMA92 Sep 30 '14 at 18:02
  • @Borgleader I need the path of the cpp file that is being executed. I am trying to list the other files from that path which can be done once I get the cpp path – VMA92 Sep 30 '14 at 18:04
  • 1
    @VinceAbruzzese please edit those important details into the question! That makes clear that you do not need the path for the `.cpp` file, but the compiled `.exe` file. – Csq Sep 30 '14 at 18:04
  • @Csq it is in fact the .cpp file I am looking for – VMA92 Sep 30 '14 at 18:05
  • 2
    @VinceAbruzzese The cpp file does not get executed, it gets compiled to an executable. Use boost::filesystem. – Borgleader Sep 30 '14 at 18:06
  • @Borgleader Yes I understand that, but for my ftp program I need to get the list of txt, pdf, etc files which are located at the same path as the .cpp file. This is why I want the .cpp path and not the .exe path – VMA92 Sep 30 '14 at 18:07
  • 1
    Is there a modern cpp way to do this? – simplename Apr 20 '22 at 01:20

3 Answers3

7

What about this??

#include<iostream>
#include <string>
using namespace std;

int main()
{
    string file_path = __FILE__;
    string dir_path = file_path.substr(0, file_path.rfind("\\"));
    cout<<dir_path<<endl;

    return 0;
}
SHR
  • 7,940
  • 9
  • 38
  • 57
  • As far I understand, the OP's real problems can't be resolved by just using the source file path. – πάντα ῥεῖ Sep 30 '14 at 18:49
  • But It is exactly what he asked, now he should take the path and use `FindFirstFile` `FindNextFile` etc. and create the bundle. – SHR Sep 30 '14 at 18:51
  • And why should the same structure for source exist on a target deployment machine? The OP missed that important point in their question. – πάντα ῥεῖ Sep 30 '14 at 18:54
  • I just replied the question been asked, not saying it is the perfect solution. Maybe he want to create an install program, to be run on the build machine, not on the target? – SHR Sep 30 '14 at 18:57
  • And I'm just explaining, what would be the usual way, and how to do this properly. Referring to `__FILE__`, isn't _really_ useful in the end. – πάντα ῥεῖ Sep 30 '14 at 19:00
  • You replied to the question: "what is the best way to create an install bundle?". but it wasn't the question here... – SHR Sep 30 '14 at 19:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62206/discussion-between---and-shr). – πάντα ῥεῖ Sep 30 '14 at 19:05
  • @SHR Thanks this is what I was trying to do. For some reason however when I try this on my current cpp file I get __FILE__ as just the cpp file and not the full path. However trying on another file gives me the full path. Any suggestion to why the path is not returning the full thing on my current project? – VMA92 Sep 30 '14 at 19:09
  • 2
    @VinceAbruzzese As mentioned in my answer, you're barking up the wrong tree here, and will get into serious trouble deploying your program at a different machine Just taking the simplest proposed solution, doesn't make it the right choice. – πάντα ῥεῖ Sep 30 '14 at 19:21
  • 1
    http://stackoverflow.com/questions/12378125/create-msi-or-setup-project-with-visual-studio-2012 – SHR Sep 30 '14 at 19:26
  • @SHR It doesn't even need to be a installer as you mentioned, a simple `.zip` archiv would work as well. – πάντα ῥεῖ Sep 30 '14 at 20:43
3

As you state in comments:

"... but for my ftp program I need to get the list of .txt, .pdf, etc. files, which are located at the same path as the .cpp file."

You should have a kind of resource directory defined, that is created when your application is installed, and where your .txt, .pdf, etc. files will be copied by the installation process.

This way implies you have an installation process (which could be the simplest form, by just creating and extracting a .zip archive), that bundles the executable .exe file along these resource files, to be installed on the target machine. Usually, you won't include the source files there.

It's actually a bad idea, to refer to the location of your source .cpp file in this case, since the finally installed program usually doesn't have a notion of the source it was compiled from. Also the program may be installed on a machine with a different environment (especially development environment) from yours.

A better entry point for a generic installation path, is to refer relative to the path that's given you with the argv[0] argument of your main() program entry. It provides you with the full path of your installed executable file.

For the rest, use the operations available from e.g. the boost::filesystem library to manipulate path and file names in a portable way, as mentioned in comments.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

Linux:

#include <string>
#include <iostream>

int main()
{
    std::string file {__FILE__};
    std::string directory {file.substr(0, file.rfind("/"))};
    
    std::cout << directory << std::endl;
    return 0; 
}

This would give you the directory containing the source file, relative to where its being built.

simplename
  • 717
  • 7
  • 15