7

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.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
newbie16
  • 767
  • 5
  • 13
  • 23
  • 1
    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? – Some programmer dude Mar 15 '17 at 15:11
  • 4
    It appears this is not supported yet: https://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 file – 0x5453 Mar 15 '17 at 15:11

3 Answers3

10

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

Miguel Duran Diaz
  • 302
  • 1
  • 3
  • 12
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

Vishal Singh
  • 1,341
  • 14
  • 13
  • 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: https://programmersought.com/article/47212852083/ – Omar Fendri Jul 22 '21 at 22:21
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...

Kevin
  • 16,549
  • 8
  • 60
  • 74
Tim Zhang
  • 13
  • 4