IntelliJ - CLion : What should I do to use input text file?
Asked Answered
E

3

8

I want to read .txt file in my C/C++ project in CLion IDE.

I want to automate the command that I run in bash:

./<executable_file> < input.txt

I edited program parameter in Run/Debug configuration.

picture

But it does not works.

Ezra answered 15/3, 2017 at 15:9 Comment(2)
Redirection is part of the shell not something that happens automatically when running a program. Maybe you should invoke a shell instead, that runs your program with the redirection? Or modify your program to handle command-line arguments?Trudeau
It appears this is not supported yet: youtrack.jetbrains.com/issue/CPP-3153 You may need to pass the file name as an argument string and let your application handle opening and reading from the fileArela
U
11

If someone is still interested in this, at the current date, CLion has added this feature. Go Edit Configurations (top right corner toolbar, name of your project | Debug): enter image description here

Now there is an option: "Redirect input from". Just click the folder and find your input file. enter image description here

Universal answered 3/8, 2020 at 17:39 Comment(0)
S
2

It is not officially supported as of now, you can do the following.

If your input file is input.txt, you can use freopen to set stdin file as input.txt

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

if you want to do the same with your output:

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

this will work for std::cin (if using c++), printf, etc...

This will help you in debugging your code in clion

Similar Question CLion standard input while debugging

Selfrenunciation answered 25/2, 2020 at 18:59 Comment(1)
Worth to mention: don't forget to use the relative or absolute path in case you are using C/C+​+​ Single File Execution plugin otherwise the compiler will generate an error. useful: programmersought.com/article/47212852083Hist
D
0

Not a direct solution but the alternative you can do using your command line.

g++ -Wall -ggdb filename.cpp -o filename

and run it using:

./filename < inputFile

Also, I don't think JetBrain has an official answer to this type of issue for Clion yet...

Desultory answered 4/12, 2019 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.