How to output to the console in C++/Windows
Since you mentioned stdout.txt I google'd it to see what exactly would create a stdout.txt; normally, even with a Windows app, console output goes to the allocated console, or nowhere if one is not allocated.
So, assuming you are using SDL (which is the only thing that brought up stdout.txt), you should follow the advice here. Either freopen stdout and stderr with "CON", or do the other linker/compile workarounds there.
In case the link gets broken again, here is exactly what was referenced from libSDL:
How do I avoid creating stdout.txt and stderr.txt?
"I believe inside the Visual C++ project that comes with SDL there is a SDL_nostdio target > you can build which does what you want(TM)."
"If you define "NO_STDIO_REDIRECT" and recompile SDL, I think it will fix the problem." > > (Answer courtesy of Bill Kendrick)
How to read output of a console application launched by a windows service
I've got an answer from another forum. "inherit handles" should be set to TRUE.
How to show the console on a C# program with Windows application output type
I have not found a way to do exactly as you are asking, but you can have a console open up depending on the input. For example:
class Program
{
private const string Kernel32_DllName = "kernel32.dll";
[DllImport(Kernel32_DllName)]
private static extern bool AllocConsole();
static void Main(string[] args)
{
if (args[0] == "/")
{
AllocConsole();
Console.WriteLine("Details");
Console.ReadKey();
//cases and such for your menu options
}
That will open a console that you can use if you follow the run command with a / even though the output type of the project is a Windows Application.
An alternative way to get console output of system() function
As mentioned by @Charles Duffy, and @Kevin, system()
is not the function you want. popen()
is more suitable. The following should work. Please note, if you are using gcc
and compile with -std=c99
flag, you need to add #define _POSIX_C_SOURCE 2
before #include <stdio.h>
#include <stdio.h>
#include <error.h>
#define PATH_MAX 1024
int main(void)
{
FILE *fp;
int status;
char path[PATH_MAX];
fp = popen("iptables -t filter -L", "r");
if (fp == NULL)
{
perror("popen");
return -1;
}
while (fgets(path, PATH_MAX, fp) != NULL)
{
printf("%s", path);
/* do something you want with the return data */
}
status = pclose(fp);
if (status == -1)
{
perror("pclose");
}
return 0;
}
Can't see Windows program's stdout in console (compiled with Clang on Windows)
A quick stdout-enabler for otherwise GUI apps:
if (AllocConsole())
{
FILE* fi = 0;
freopen_s(&fi, "CONOUT$", "w", stdout);
}
and then std::cout
and printf
work.
How to get a console window from a C++/MFC program?
You must use AllocConsole - either a windows program is console, or it's not. You can do so e.g. in CWinApp::InitInstance, since WinMain is abstracted away for you by MFC. But you can do it from anywhere really.
Related Topics
A Free Tool to Check C/C++ Source Code Against a Set of Coding Standards
Floating Point Keys in Std:Map
What Kind of Optimization Does Const Offer in C/C++
How to Remove/Refactor a «Friend» Dependency Declaration Properly
Why Is the Size of an Empty Class in C++ Not Zero
C++ Static Member Initialization (Template Fun Inside)
How to Automatically Convert Strongly Typed Enum into Int
Does Const-Correctness Give the Compiler More Room For Optimization
Is Calling Destructor Manually Always a Sign of Bad Design
Why Is 'Std::Move' Named 'Std::Move'
Heterogeneous Containers in C++
Throwing Exceptions from Constructors
Why Are String Literals L-Value While All Other Literals Are R-Value
Why Is My Integer Math With Std::Pow Giving the Wrong Answer
Is This Key-Oriented Access-Protection Pattern a Known Idiom