C Program to Hide a Console Window On Startup
Last Updated :
02 Jan, 2023
Here in this article, we have created a console application that will write into a file. This console application will work in the background by hiding the console window. To ensure that the program or the console application is still running, we can see the live data appended in the text file created by the program.
Console Window
The console is literally “a window” to the world of the computer. It works on the concept of a text-only interface. The programs that run via the console are called the Console Application. Consoles are often called command Line interfaces, Command Prompts, or Command Terminals.
Console Application vs Window Application
The Console application and the Windows application are very similar to each other. Both are attached to a Console at some point in their lifespan. The Console application does not return the console until specifically mentioned but the Windows application immediately returns and frees the command prompt or the console.
Example: Here, we have created a C program that will create a text file and write some text to it. Program execution will take twenty seconds as it will sleep for two seconds for each write operation on the file.
C
#include <stdio.h>
#include <windows.h>
int main()
{
FILE * f;
int i = 1;
while (i < 10) {
f = fopen ( "testing.txt" , "a" );
if (!f) {
printf ( "File not created!" );
}
else {
printf (
"%dSuccessful creation of the file named "
"as testing.txt\n" ,
i);
fprintf (f, "%dHELLO FROM GEEKSFORGEEKS\n" , i);
i++;
}
fclose (f);
Sleep(2000);
}
return 0;
}
|
Output:
Text appended to file in C
In the above program, Opening the text file multiple times within the timespan of 20 seconds will show you that the file is being updated, but the console window is also present side by side.
Hiding Console Window
Now if we need to hide the console window and still want to append the text to the file, we need to do some programming that will execute the commands to hide the console window. Here we will use the “window.h” header file. At first console window will hide then the program will write to the file and after the writing is over the console is shown again.
Example:
C
#include <stdio.h>
#include <windows.h>
int main( void )
{
HWND console;
AllocConsole();
console = FindWindowA( "consoleWindowClass" , NULL);
ShowWindow(console, SW_HIDE);
Sleep(5000);
FILE * fp;
int i = 1;
while (i < 10) {
fp = fopen ( "testing.txt" , "a" );
if (!fp) {
printf ( "File not created!\n" );
}
else {
printf (
"%dSuccessful creation of the file named "
"as testing.txt\n" ,
i);
fprintf (fp, "%dHELLO FROM GEEKSFORGEEKS\n" , i);
i++;
}
fclose (fp);
Sleep(2000);
}
ShowWindow(console, 1);
return 0;
}
|
Output:
The console reappears at the end of the program.
Condition 2: If you don’t even want the console after the execution of the program, then delete the “ShowWindow(console, 1);” from the last line of the program.
Output for the second condition:
Console completely hidden
Advantages of Hiding Console
- Some programs do not need user interaction, so no need for a console window.
- The program can run in the background, without interrupting the user.
Disadvantages of Hiding Console
- Progress of the program will not be visible.
- Console inputs will hang the program.
- Need to kill the program from the task manager to close the console app if continuously running.
Share your thoughts in the comments
Please Login to comment...