Tabish Saifullah
Tabish Saifullah

Reputation: 570

Why printf not giving output in a windows form C++ application

I created a simple windows form application C++ project in MS VS 2010. Then I intent to print to console:

// FtoC.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"
#include <stdio.h>
#include<conio.h>

using namespace FtoC;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    printf (" printing to console"); 


    // Enabling Windows XP visual effects before any controls are created
    //Application::EnableVisualStyles();
    //Application::SetCompatibleTextRenderingDefault(false); 

    // Create the main window and run it
    //Application::Run(gcnew Form1());


    getch(); 
    return 0;
}

As you can see I have commented everything except my printf statement.

It compiles without any error, but no output is coming. Why it is so?

I modified the code as shown below:

#include "stdafx.h"
#include <Wincon.h>
#include "Form1.h"

#include <stdio.h>
#include<conio.h>


using namespace FtoC;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
  {

   BOOL chk = AllocConsole();
if(chk)
{
  freopen("CONOUT$", "w", stdout);
  printf (" printing to console"); 
}
  else

{
    throw new SomeException();
}
    getch(); 
    return 0;
}

But now I am getting lots of errors in wingdi.h file, such as:

Error   270 error C1003: error count exceeds 100; stopping compilation  C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\wingdi.h  750 1   FtoC


Error   159 error C2065: 'MAX_PATH' : undeclared identifier C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\wingdi.h  683 1   FtoC

What went wrong ??

Upvotes: 0

Views: 2486

Answers (1)

deeiip
deeiip

Reputation: 3379

It will not show you any output because WinForm does not have a console attached to them. Now if you really want to use a console

BOOL chk = AllocConsole();
if(chk)
{
    freopen("CONOUT$", "w", stdout);
    printf (" printing to console"); 
}
else
{
    throw new SomeException();
}

Ref AllocConsole()

Upvotes: 2

Related Questions