How to automatically close window after execution? - c

How can I close the command window in C ? exit(-1) doesnt work , it just returns -1 . I want the window to close without pressing any key . I'm using CodeBlocks in Win 8.1.
int main (int argc, char **argv)
{
Commands();
exit(-1);
}

I am assuming this is windows as you said command window, not terminal? Have you tried FreeConsole?
If you open the console from windows and run your app inside it, then you cannot close it, because the console only closes when all processes which share it detach... and the windows process which created the console would still be attached.

I was trying to make the CodeBlocks window close after execution , but it seems that if i execute the code from command line it closes after execution . Thanks.

Related

a.exe terminal closes just after program execution stops [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to stop C++ console application from exiting immediately?
I created an exe file in c. When I run it command prompt opens and then closes quickly and I cannot see the output. The program takes no runtime values from users. It reads data from a file. Is there any way to prevent this?
Run it natively from the command line.
Let's say that your file is in C:\awesomeness.exe
Open the cmd, type cd C:\ and then type awesomeness.exe
One classic way to do it:
#include <stdio.h>
int main() {
puts("hai");
getchar();
}
This will wait for keypress at the end.
Are you in Windows? If so, a quick and dirty way is to use the system() function in stdlib.h (make sure you include it) to execute the PAUSE command in the command prompt.
system("PAUSE");
If you are using Visual Studio, hit CTRL + F5 instead of F5 in order to run your program.
Using 'system("pause");' before ending main() is the most common method.
int main() {
...
...
system("pause");
}

Prevent command prompt from closing after the code run

I have Microsoft Visual Studio and after I write a code and debug it the command prompt closes directly after the code run. Then I used
getc(stdin)
getch()
But all these also closes it after a click, Is there anyway to unable it from closing untill I click X?
There is an option to keep the output window open after run is finished, though it's pretty similar to what you did(adds a "Press any key to continue . . .")
If you want just to hold the window just add at the end
Sleep(100000000);
It will hold the window for this many mili seconds.
If it's still not enough do
while(1)
{
Sleep(100000000);
}
It will hold the window indefinitely

Unable to input data to C program while debugging in separate terminal with gdb & tty command

I am debugging a C program with gdb. I have used the tty command to send the output to a new terminal window but am unable to enter input while the program is running.
If I debug in the same window it works fine, but when using a separate terminal window, input doesn't do anything.
I can still kill the process with ctr-c but once I hit the input line, it waits for input and doesn't do anything when I press return.
I have looked around but am haven't found the same problem online. Any ideas?
Start your program in one window. While your program is waiting for input, start gdb in another window. Use the gdb attach command to attach to and debug your program.

Is there a command that will allow a user to "type" from the command line?

I have been exploring the system(""); command in C. It seems that it directly does what a GUI can do from the contexts of a program, except for actually "typing". Is there a command line keyword that will "type"? For example, you use system("notepad.exe"), which will open a blank notepad, with the cursor blinking eagerly for input. I was wanting to "type" into this blank notepad through the command line. I've played around with things like 'system("print abcde");' , 'system("type abcde");' and I cannot find any command that will do what I want in my searching. Does such functionality exist in the Windows command prompt?
Let me try to demonstrate what I am trying to do:
#include <stdio.h>
int main(void)
{
system("dir");
system("notepad.exe");
system(/*This is where I need to put the code that will type to notepad */);
return 0;
}
I just want a baby program that can automate printing to notepad basically.
You might have to use WIN32 API for this, you can Get a handle on the notepad, and send the input from the console, to the notepad window using SendMessage() to the notepad's handle.

Why does my program's output flash and close in Windows?

I'm trying to build an .exe file for the K&R "Hello, world". The code given in the book is:
#include <stdio.h>
main()
{
printf("Hello, world!\n");
}
When I build & run from Code::Blocks (under Windows XP), I get the prompt window with the "hello world" message. It stays open until I close it manually. However, when I double click the .exe file, the prompt just flashes and disappears, why is that?
No one is explicitly telling you this, so I will:
What you see when you double-click the file is normal. What your IDE does (keeping the window open) is a feature to help you debug the application.
Why is this so?
Since you're developing a console application, there has to be a console for your application to display its output on. If there is none yet, a new console is created (which is the black window).
If you launch your program from inside a console (say, from cmd.exe), it will just inherit the console of the parent without creating a new one[1].
After the last application using the console exits (which, in the first case is just your program), the console closes. You will notice this all the time for console applications that print nothing but a help text when run without parameters. If you double-click them from explorer, a black window with some text will flash and then immediately close.
Sometimes, a program that does something and them immediately closes is what you want. For example, you can call these applications from scripts.
On the other hand, your application could be interactive: waiting for user input, doing some thing, and only exiting when the user tells it to. You cannot script these applications, obviously, as you will need to have a human present at the keyboard to tell the application what to do.
Now we get to the IDE part: let's say you're developing an application of the first kind, one that does something and then immediately closes. It's not very convenient to have the screen flash and disappear every time you run it, because how can you tell if the program worked? Assuming you can tell this from the output it generates.
You could of course start a command-line window and run the application from there, but the program would execute separately from the IDE, and you would lose live debugging capabilities.
So, IDE makers came up with a feature for console applications: when you run the application directly from your IDE, they afterwards, usually waiting for a keypress. This gives you the opportunity to inspect the window with the output, to confirm that the application is working properly.
[1] Esoterica: unless you go through an application that does not inherit the console. Any console app launched by that application will not inherit the console, since the inheritance was broken by the GUI app. For example, start.exe does this. Compare:
foo.exe (inherits the console)
start foo.exe (start.exe is a GUI app, so foo.exe is launched in a new console)
If you're not running a command line exe from an already open command line window, Windows will automatically close the windows after the program has terminated. Try opening cmd.exe, navigating to your program's directory and running it from there, the window should stay open.
When running from IDE's like this, they run the program and when its done running, they close it.
Since your program's only function is to print out a value, it does that and closes.
You should try to add something that asks for user input at the end or compile it into an .exe and run itself from the command line yourself.
Since you are starting I would recommend to just run it from the command line yourself. You will eventually learn about user input and there you can have the command line window open when you use your program.
Normal behavior.
Your program executes every action in the order of the main. So it prints, and then moves on to the next operation, there is none, so it exits. Since the console window is tied to your .exe, the command window closes with the program.
If you don't want your program to exit right away, you can make it sleep, or wait for user input before exiting.
When double clicking a .exe in Windows, you are launching a new process. Windows has 2 basic process types: Window and Command line. The hello world sample you've written is a command line process.
A command line process will launch a new command window on startup. This is the window that pops up which is largely a black background with white text. Upon completion of a program the window will close down.
Add getch(); before the closing brace. This will prompt for an input after the output is printed. Once you key in a character the window will close. This should solve your problem.
The preferred solution is to run the executable from the command line.
Try running your binary from the command line.
That is because the executable file opens its own dialog box. When the executable has completed running it shuts down the dialog box that it opened in order to run. However, when YOU are the one that opened the dialog box, it disappears when YOU close it.
So if you were to open up a command prompt and then run the executable, the dialog box would not automatically close.
That is because from the executable, it executes your code in a new window and then the process is done, it has no reason to stay open, what you wanted to do is complete. There are a couple of things you can do. You can execute it from the cmd.exe command line, or you could even put something at the end of your code that listens for a key press, and once the key stroke is detected, allow the program to exit.
just add
system("pause");
line before return. it`s not the best, but universal method.
Here is my take on this:
// Hello sweetie (Spoilers)
#include <iostream>
using namespace std;
int main()
{
// Print the text to screen
cout << "************************************";
cout << "\n";
cout << "Hello World!";
cout << "\n";
cout << "You may close me by pressing Enter";
cout << "\n";
cout << "************************************";
cout << "\n";
cout << "\n";
/*
This will prompt for an input after the output is printed.
Once you hit the Enter key the window will close.
*/
if (cin.get() == '\n')
return 0;
}

Resources