Ofstream files not being made when argument file is in a different directory than current directory

  c++, cmake, file-io, ofstream

To compile my program, I use a written cmake file like below:

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(GARFIELD_SIMULATIONS)

#---Find packages-------------------------------------------------------
find_package(Garfield REQUIRED)
find_package(OpenMP REQUIRED)

#---Build executables-----------------------------------------------------------
add_executable(test1 test1.C)
target_link_libraries(test1 PRIVATE OpenMP::OpenMP_CXX Garfield)

add_executable(test2 test2.C)
target_link_libraries(test2 Garfield)

This compiles my programs without any issues. However, I am having problems creating output files when I run my executables. Specifically, test1.C takes in a file as input argument and produces an output file by doing something like this:

string filename = argv[1];
ifstream input(filename, std::ios::in);
double x;
vector<double> xs;
while (input >> x) {
  xs.push_back(x);
}

ofstream output("output_of_"+filename, std::ios::out);
double y;
for (int i = 0; i < xs.size(); i++) {
  y = xs.at(i) + 1;
  output << y << "t";
}
output.close();

Now, the issues arrive when I try to pass in an argument that is not in my current directory. For example, if I do something like this:

./test1 file1.txt

then I would get the output file. However, if I do something like:

./test1 /some/other/directory/file1.txt

I will not get any output file. I tried to search for the output by using find, but that returns nothing. I am still new to cmake, so I apologize if this is something obvious. I tried to use Google but nothing helpful has shown up yet. Any help is greatly appreciated.

Source: Windows Questions C++

LEAVE A COMMENT