1

I need to be able to debug and redirect input to stdin in CLion, but I can't find a way to do this. The only solution I can think of is to set stdin to be the file.

I looked into using freopen() but this seems to be invalid in C99, which I have to use for the class I'm in.

2
  • Although I don't really understand what you mean, give a look at dup2, it may help Commented Sep 22, 2019 at 4:25
  • I need to be able to do the command ./program.out < input.txt (like you can do normally by CD into the cmake-build-debug folder, but this doesn't work for debug. The idea is to set stdin to a file manually instead of redirecting input via. the terminal, but I'm not sure how to do this. Commented Sep 22, 2019 at 13:41

1 Answer 1

1

don't read stdin in your program

FILE *input = stdin; if (debugging) input = fopen("somefile", "r"); // ... use `input`, not `stdin` if (debugging) fclose(input); 
Sign up to request clarification or add additional context in comments.

1 Comment

The following works: freopen("input.txt", "r", stdin); but when I use fgetc() from debug, a value of -1 is returned, and I believe that means it isn't able to get a valid value from the file (the file IS valid). Everything else works fine when I run it with redirected input from the console (./program.out < input.txt)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.