Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLion standard input while debugging

Tags:

c++

clion

What I'm trying to do is basically:

./myProgram < myData.txt

While I'm debugging with CLion IDE. I just can't find the option to do so.

A similar question - but product-specific to MSVS

like image 456
user5414301 Avatar asked Oct 06 '15 13:10

user5414301


People also ask

How do you debug in CLion?

For debug press Shift+F9 . To help you inspect the state of your code during debugging, CLion equips you with many useful shortcuts like Step over/into ( F8/F7 ), Step out ( Shift+F8 ), or Run to cursor ( Alt+F9 ).

Does CLion use GDB?

CLion supports debugging C/C++ executables with GDB (either bundled or custom) on all platforms and with the bundled LLDB on macOS and Linux. Also, there is an LLDB-based debugger for the MSVC toolchain on Windows.

How do you set a breakpoint in CLion?

Set line breakpointsClick the gutter at the executable line of code where you want to set the breakpoint. Alternatively, place the caret at the line and press Ctrl+F8 . While in disassembly view, you can set breakpoints the same way you do in the source code.

How do you run in release mode CLion?

For newer versions: Go to File --> Settings --> Build, Execution, Deployment --> CMake. Now click the "+" symbol, this should automatically add a Release profile (and, if you press "+" again, a Release with Debug Information profile).


3 Answers

I had the same problem and it seems that CLion is not handling standard inputs yet.

I got around this problem by changing the input stream before running my program.

As an example if you want to input a file stream inside your stdin you can write in your main:

std::ifstream in("ABSOLUTE_PATH_TO_YOUR_FILE");
std::cin.rdbuf(in.rdbuf());

Then you can find a way to toggle this stream change when you want. Note that for files you will need to provide absolute path since the application is run from a different directory than the current one.

I hope this can help until CLion provides a real solution.

like image 120
Arnaud Bertrand Avatar answered Oct 11 '22 18:10

Arnaud Bertrand


Assuming your input file is myData.txt, you can reopen/reuse the stdin stream using freopen

freopen("myData.txt","r",stdin);

if you want to do the same with your output:

freopen("myOutput.txt","w",stdout);

this will work for std::cin, printf, etc...

You can find more information about this here: http://www.cplusplus.com/reference/cstdio/freopen/


By the way, there is already a feature request for this. If you are interested, you can vote here so it gets prioritized: https://youtrack.jetbrains.com/issue/CPP-3153

like image 22
Juan Leni Avatar answered Oct 11 '22 18:10

Juan Leni


As of CLion 2020.1 this feature is built in:

Input redirection

If you need to redirect input from a file to the stdin of your application, you can now do that. Use a new field in the configuration called Redirect input from. Enter:

  • A relative path (CLion will prepend with the Working directory path).
  • An absolute path (will be remapped for remote configurations).
  • Or macros (like FilePrompt). enter image description here
like image 4
Ofek Shilon Avatar answered Oct 11 '22 17:10

Ofek Shilon