How to change directory cmake uses for reading/writing to files?

At the current moment my project struture in clion is along the lines of:

-main.cpp
-CMakeLists.txt
-cmake-build-debug/example.txt

I notice that when i want to read a file and I call

 
    std::ifstream inputdata("example.txt");


and it works.
But if my files are structured like this, it wont work:

-main.cpp
-CMakeLists.txt
-cmake-build-debug
-example.txt

I'm not sure how to configure cmake to make it read files from where the source (main.cpp) is located instead of having to be inside the cmake-build-debug folder.

Last edited on
Hello bigpops,

I am not the pest person to answer this, but at least I will bump it to the beginning so that someone that knows more might see it.

I am not familiar with "CLion" yet, so I am not familiar with the files and how it creates the ".exe" file.


Based on your statement:

I notice that when i want to read a file and I call

    std::ifstream inputdata("example.txt");

I believe you have the wrong understanding here. The above line creates a file stream between the program and the file. Similar to the "cout" and "cin" streams to the display and keyboard. In simpler terms you can think of it as it opens a connection to the file only. A line of code: inputdata >> aVariable;, I would suggest using the camel case method (inputData), actually reads the file.

My limited knowledge of the "cmake" file is that it automates the process of compiling the ".cpp" files into object files, those that end in".o" and then links the object files with the needed libraries, files with a ".a" extension, to create the filal ".exe" file.

If you have not seen this I did find this page that might help https://www.jetbrains.com/help/clion/managing-cmake-project-files.html

I have not had the time to read through all of it yet, but it looks useful.

I will install CLion and give it a try to see if I can figure anything else out.

Hope that helps,

Andy
Topic archived. No new replies allowed.