27

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

Community
  • 1
  • 1
user5414301
  • 271
  • 1
  • 3
  • 3
  • You should have more luck asking this question in appropriate dev's [forum](https://devnet.jetbrains.com/community/clion?view=discussions) – Dialecticus Oct 06 '15 at 14:06

5 Answers5

25

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.

Ofek Shilon
  • 14,734
  • 5
  • 67
  • 101
Arnaud Bertrand
  • 1,341
  • 10
  • 13
  • 2
    If you don't want to do it with a file, remember there is the `stringstream`s which you can use – smac89 Jan 11 '18 at 05:34
12

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

Juan Leni
  • 6,982
  • 5
  • 55
  • 87
5

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
Ofek Shilon
  • 14,734
  • 5
  • 67
  • 101
1

Still Clion don't have the feature like pycharm where we can give input in terminal while debugging the code.

But it has an option to give input through a .txt file while debugging.

Image of debug setting window

Click the setting icon in the debug console (on upper left corner) to open the setting of debugging. Then check the "Redirect input from" box and select the input file path and click "OK".

Here you go!

Now you can give input from the text file while debugging the code.

-3

For me, CLion creates the executable in a file called 'cmake-build-debug'. Check out my file structure in the pic.

Executable File Relative To Text File

Then, I just opened up my terminal and went to the directory containing the executable and used this command to pipe in the text file:

./FirstProject < ../hw1.txt
zhughes3
  • 467
  • 10
  • 22