1

I am using IBX cplex solver in my C++ program. by default, it prints the solve output into console. But, i wanted this solver output as a string, which can later be used for logging or cout. To achieve this, i used the below code snippet (with setOut function provided by IBM Cplex), but it prints nothing. Please advise me what would be the correct way to achieve this?

//Other code stuffs to initialise cplex solver and model
std::ostringstream ss("");
_cplex.setOut(ss); //_cplex is an instance of IloCplex
std::cerr << ss.str() << std::endl; // Tried ss.rdbuf() also, didn't help

However, It can write the output content into a file with the below code,

//Other code stuffs to initialise cplex solver and model
std::ofstream of("dummy.txt"); //_cplex is an instance of IloCplex
_cplex.setOut(of); //Write successfully to the file dummy.txt
Karthick
  • 357
  • 4
  • 13
  • 2
    Using CPLEX 12.8 on x86-64_linux, if I modify the ilolpex1.cpp example to use an `ostringstream` as you did above, it works fine. I suspect something else is at play here. You are calling `_cplex.solve()` before printing the output, correct? Can you try making the same modification to ilolpex1.cpp and see if that works for you? – rkersh Feb 11 '19 at 20:34
  • Thanks for the hint @rkersh , I made a bad mistake. It was due to incorrect placement of output print (i.e., printed the output before the solve()). It works fine when i print the output after the solve(). Thanks! – Karthick Feb 12 '19 at 02:10

0 Answers0