0

I have a problem with a project in Visual Studio. The project is created as an empty project, and then a .c file was added. The problem is that the console closes immediately after the program ends when I redirect input to a file.

I tried going to Properties > Linker > System and selecting /SUBSYSTEM:CONSOLE option, but it doesn't solve this. This always worked for me, but now when I redirected the input, the console closes right after the program execution and I can't see the output.

I redirected the output by adding <"in.txt" in Configuration properties > Debugger > Command, and it works exactly the way I wanted, except the console closes too soon. This problem doesn't occur when I redirect the output.

Also using getchar(), scanf(...) or system("pause") didn't work.

I would love to solve this by only changing some project settings and without adding some extra code to a program if possible, but any solution is appreciated.

Edit: As I stated above, I have tried several things, including some answers from similar questions, but none of them helped.

Urke
  • 99
  • 7
  • Would `GetFileType(GetStdHandle(STD_INPUT_HANDLE)))` help? This will return `FILE_TYPE_DISK` (`0x0001`) if you redirected a file, or `FILE_TYPE_CHAR` (`0x0002`) for console input. One thing is also unclear: you are basically asking why *Visual Studio* closes the console when the program is done? – vgru Apr 19 '18 at 14:48
  • I will definitely try that. Sorry I wasn't clear enough, my question is how to prevent this from happening, if possible. In the answer below, Felix explained what is actually going on there. – Urke Apr 19 '18 at 14:59
  • 1
    I don't think the suggested duplicate matches here, there's nothing about redirected stdin over there. –  Apr 19 '18 at 15:12

1 Answers1

1

It's standard windows behavior that a console program automatically gets a new console window when not started from an existing one and this new window is automatically closed as soon as the program ends. So the key to force the window to stay open from withing your program is to prevent program termination.

With things like getchar(), you rely on the fact that these calls block until more input is available. This works fine as long as input actually comes from the console. But when you redirect input to come from a file, there will always be input available -- either there's more to read from the file or you'll instantly get an error (e.g. for EOF). That's the reason all these "tricks" don't work any more.

A simple and very unclean version is to just add an empty loop to the end of your program, so it never exits. On the windows platform, this could be something like

for (;;) Sleep(1000);

The call to Sleep() (include windows.h to use it) makes sure this loop doesn't burn CPU cycles. You'll have to forcefully end your program by hitting Ctrl+C or closing the window. Of course, don't leave this loop in your final program, it would be quite annoying when started from an existing console window.

There might be better solutions to this problem, but I hope I could explain you why this is happening.

Developer Guy
  • 2,318
  • 6
  • 19
  • 37
  • Thank you very much, at least now I know what is going on. I knew that those `getchar()` approaches wouldn't work with this, but I had to try anything, and I wrote it so people don't suggest me something I have already tried. Just one more thing, are you saying that in order to solve this, I will have to prevent program from terminating, or is it just an idea? I'd just like to know whether I should keep looking for an answer or settle with this. Thanks again, for quick and precise answer. – Urke Apr 19 '18 at 14:32
  • It's a fact that as soon as your program terminates, the console window created for it is gone. You could of course try to let visual studio run your program inside `CMD /k [command]`, in this case, CMD will keep running after your program terminated, so the window would stay. I'm not sure this is feasable though ... –  Apr 19 '18 at 14:37