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.
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.
Related
I created a program in C, then clicked run in VS Code, it was about to scan string from user. I typed the string and press enter, then I am seeing something I cannot able to understand.
The program finished. It did not finish as you expected but it ran to the end. I would suggest two things. Click on the tab that says "problems" it will tell you about some bugs in your code. Step thru the code one line at a time instead of running the code with no breakpoints.
So, I've written a C executable on Linux for Windows using mingw32-gcc. It is a basic Input-Output program, you type an input and get an answer.
Problem is the cmd shuts down immediately, so the user can't see the output.
Assuming I cannot use Windows to edit the executable there, what should I change in my code/ what flags should I use when compiling it?
inb4:
the user is supposed to click and run it, so running it from cmd won't help.
adding getchar()/scanf() at the end of my code doesn't work, and it feels a bit like cheating.
SOLVED: so all I had to do was to actually add a getchar() after every scanf() and one more at the end of the code for the user input to close the cmd.
Waiting for input at the end is not cheating, but common practice. How else should program know for how long it should stay opened? Closing program by closing console window directly is more cheating than waiting for user input to finish.
You can for example prompt user to hit any key like Press any key to exit... or something similar.
If you want some specific delay, you can use Sleep() from windows.h instead of waiting for input.
See https://stackoverflow.com/a/3379146/3035795
I've been coding for a while with codeblocks and I've coded some C programs. In my project folder of my programs there is the .exe file that I used to click on and get its console output. But today when I click on them and when the program ends it closes instantly without asking me to press a key to quit the program.
The solutions for this problem I've found are to add "system("pause")" and other stuff like that or to compile it directly on the cmd or on codeblocks(and they don't fix the problem as well). But I would like to know how I could be able to open the .exe files like back then.
did you try something like that at the end of your program?
scanf("%c",waitVar);
You can write
printf("Press ENTER key to Continue\n");
getchar();
at the end of your code. The same question can be found here:
how to prevent c program from closing immediately after running
I think this is simple, but not for me obviously!
I have a console-application. I need to read input from the keyboard, but stdin has been redirected to a file. So how do I create a FILE-Handle that points at the keyboard-stream which i can use with fgets etc.?
I found out that ttyname(0) seems to be what i look for in a POSIX-environment, which I don't have here. I'm in a Windows-system with standard Visual Studio compiler.
Any ideas? Thank you in advance.
There's no easy/portable way to tell if a keyboard exists (your application may be being run from a terminal emulator from a serial port, a telnet session or anything else). If a keyboard actually does exist (including a picture of a keyboard on a touch screen), then you can't really tell how many layers of software the keystrokes need to pass through before they get to your application (e.g. keystrokes might go from a keyboard driver to an input method editor to a GUI to a shell to your application). This means that attempting to get keystrokes directly from a keyboard driver or something is a bad idea that will fail in almost all cases.
The best way to solve your problem is to find out which series of design failures led to STDIN being redirected in the first place.
For example; maybe the application should've had a command line option to read some data from a file; so that the application can get some data from the file and some from STDIN (and get all data from STDIN if the command line option isn't present).
Pulling from the dim dark days of DOS programming here: try opening "CON:" (Console), a reserved word. Hopefully it will open the same way in Windows. The colon may or may not be required. Both "dir >con:" and "dir >con" still work in command prompt.
Also, be sure to use something from the setbuf() family on the output handle to avoid buffering... it's not supposed to buffer terminal I/O, but it never hurts to be sure.
Again, not sure, but I suspect opening separate FILE *conin, *conout for output and one for input may help if you seem to have troubles with one handle doing both input and output.
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
What is the Best Practice for Combating the Console Closing Issue?
How do you keep the console from closing after the program is done in C? When I try to search for it I find lots of stuff about C++ and other languages, but nothing for C. Also, even for C++ there doesn't seem to be a definitive answer.
So could someone please let me know what the simplest way (doesn't need to be super elegant) way to keep the console open after a C program is done running?
The previous answers are all assuming that you want to invoke the console app and then essentially leave it "running" and waiting for user input to terminate. If this is the correct assumption, then +1 to GMan's answer. However, if you are asking how to invoke this console app from either a shortcut, Start->Run or some other mechanism and leave the cmd window open, then you will need to invoke it via cmd.exe itself with the /k option like so:
cmd.exe /k "foo.exe"
This will start a cmd window, run your console app, and then leave the cmd window open. This will address #Thanatos above. He's correct in that you should let the console app close. Again, for me it's unclear what you're really asking for what the end goal should be.
If I made the wrong assumption(s), feel free to -1 me.
run the program from the command
line, instead of executing it
directly.
Ctrl+F5 in Visual C++.
Console applications are meant to be run from the console. If you do that, after running you'll be left with your console window, and can easily view the output of your program.
You can use something like getchar() to force the application to wait for a key-press.
Let the console close.
If you prohibit, in the program, the console from closing, it will make automation with your program difficult, or it will make the format of the program's input strange.
Instead, fix whatever's running the program in the first place, to not close the terminal window in the first place. If this is MS Visual Studio, try F5 (Start without debugging). If you need debugging, place a breakmark at the program's end. Otherwise, open a command prompt/terminal and run the program there yourself.
1) Your IDE opens the console before the program begins.
2) your program ends
3) the IDE closes the console
a) Just tell the IDE to not close the console ... or
b) make your program not end.
a) No idea how to do it.
b) right before the return 0; used to terminate the program add
printf("Press ENTER a few times to terminate the program");
fflush(stdout);
getchar(); getchar(); getchar(); getchar();
getchar(); getchar(); getchar(); getchar();
return 0;
You could use getch() at the end of your program.
Another way is to debug the program and place a break point before the end of the program.