1

I have .in file in project directory when i use absolute path to open the file it works but relative path not working.

directoy content

main.cpp
CMakeLists.txt
milk2.in

CMakeLists.txt

cmake_minimum_required(VERSION 3.3)
project(project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES  main.cpp )
add_executable(project ${SOURCE_FILES})

main.cpp

#include <iostream>

using namespace std;

int main() {

pair<int, int> periods[5000];
int lineCount = 0;
freopen("milk2.in", "r", stdin);

cin >> lineCount;


for (int j = 0; j < lineCount; ++j) {

    cin >> periods[j].first >> periods[j].second;

}

int startNode = periods[0].first, endNode = periods[0].second, milkMax = 0, freeMax = 0;

for (int i = 1; i < lineCount; ++i) {
    if (periods[i].first <= endNode) {
        endNode = periods[i].second;
        if ((endNode - startNode) > milkMax)
            milkMax = endNode - startNode;
    } else {
        if ((periods[i].first - endNode) > freeMax)
            freeMax = periods[i].first - endNode;
    }
}

cout << milkMax << " " << freeMax << endl;

}

there is no errors in the code but can,t read files contents with relative path.

mohamed abdallah
  • 134
  • 2
  • 10
  • 1
    `when i use absolute path to open the file it works but relative path not working.` - This is because your `.in` file is located in *source* directory, but executable is created in *build* directory. Same problem is described in the [other question](http://stackoverflow.com/questions/31495311/clion-c-cant-read-open-txt-file-in-project-directory?rq=1) with very similar title. – Tsyvarev Jul 19 '16 at 07:27
  • My searches for this topic all land me here despite it being considered a duplicate. There's a nice way to control what folder the executable uses for file I/O. Please see my answer here: https://stackoverflow.com/a/48632888/165164 – Anne Gunn Feb 05 '18 at 23:03

1 Answers1

2

That is because the run-time directory is not the same as the project source directory.

The simplest solution I could think of is to define a macro that equals the project source directory, have the program change its working directory to that, and then open the files.

To do that you need to modify the CMakeLists.txt file like

cmake_minimum_required(VERSION 3.3)
project(project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES  main.cpp )
add_executable(project ${SOURCE_FILES})
target_compile_definitions(project
    PUBLIC -DPROJECT_DIRECTORY="${CMAKE_SOURCE_DIR}")

And then in your main.cpp file:

// Header file for chdir call
#ifdef __linux__
# include <unistd.h>
#endif
#ifdef _WIN32
# include <direct.h>
#endif

int main() {
#ifdef __linux__
    chdir(PROJECT_DIRECTORY);
#endif
#ifdef _WIN32
    _chdir(PROJECT_DIRECTORY);
#endif

    // Rest of code here...
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • You can change the exe's working directory without having to edit the build files. Please see my answer here: https://stackoverflow.com/a/48632888/165164 – Anne Gunn Feb 05 '18 at 23:03