简体   繁体   English

调试时的 CLion 标准输入

[英]CLion standard input while debugging

What I'm trying to do is basically:我想要做的基本上是:

./myProgram < myData.txt

While I'm debugging with CLion IDE.当我使用 CLion IDE 进行调试时。 I just can't find the option to do so.我只是找不到这样做的选项。

A similar question - but product-specific to MSVS一个类似的问题 - 但特定于 MSVS 的产品

I had the same problem and it seems that CLion is not handling standard inputs yet.我遇到了同样的问题,似乎 CLion 还没有处理标准输入。

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:例如,如果您想在 stdin 中输入文件流,您可以在 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.我希望这会有所帮助,直到 CLion 提供真正的解决方案。

Assuming your input file is myData.txt , you can reopen/reuse the stdin stream using freopen假设您的输入文件是myData.txt ,您可以使用freopen重新打开/重用stdin

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...这将适用于 std::cin、printf 等...

You can find more information about this here: http://www.cplusplus.com/reference/cstdio/freopen/您可以在此处找到更多信息: 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如果您有兴趣,可以在这里投票,以便获得优先权: https : //youtrack.jetbrains.com/issue/CPP-3153

As of CLion 2020.1 this feature is built in :从 CLion 2020.1 开始,此功能内置于

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 .在名为Redirect input from的配置中使用一个新字段。 Enter:进入:

  • A relative path (CLion will prepend with the Working directory path).相对路径(CLion 将在工作目录路径前面加上)。
  • An absolute path (will be remapped for remote configurations).绝对路径(将为远程配置重新映射)。
  • Or macros (like FilePrompt).或宏(如 FilePrompt)。 在此处输入图片说明

Still Clion don't have the feature like pycharm where we can give input in terminal while debugging the code. Clion 仍然没有像 pycharm 这样的功能,我们可以在调试代码时在终端中提供输入。

But it has an option to give input through a .txt file while debugging.但它可以选择在调试时通过 .txt 文件提供输入。

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.现在,您可以在调试代码时从文本文件中提供输入。

For me, CLion creates the executable in a file called 'cmake-build-debug'.对我来说,CLion 在名为“cmake-build-debug”的文件中创建可执行文件。 Check out my file structure in the pic.在图片中查看我的文件结构。

可执行文件相对于文本文件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM