4

I should preface with the fact that I'm relatively new to VS, however I am not new to C.

The problem I'm encountering is that nothing shows up on stdout when printed. Neither printf/_s, nor fprintf/_s(stdout, ...) produce any output. Interestingly enough fprinf(file, ...) does in fact produce output to the given file. Is there a chance this has to do with printf deprecation (I have tried the preproc. _CRT_SECURE_NO_DEPRECATE)?

Below is my full program:

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)                  
{
    FILE * pFile = fopen("outputTest.txt", "w");
    fprintf(pFile, "At top of main1.\n");       //works
    printf("At top of main2.\n");               //doesn't work
    printf_s("At top of main3.\n");             //doesn't work
    fprintf_s(stdout, "At top of main4.\n");    //doesn't work
    fflush(stdout);
    fclose(pFile);
    return FALSE;
}

I'm using Visual Studio 2017, and the program is a Win32 (App?). Also I've ruled out the possibility that Linker->System->Subsystem is the problem.

Any ideas are appreciated.

EDIT: I'm not sure if it matters but the "Solution Platforms" dropdown at the top of VS says Win32, unlike when you create a new "Windows Desktop App." where it says x86.

mpchenette
  • 127
  • 1
  • 1
  • 4
  • It is not terribly obvious why you try to use printf() in a program that is not a console mode app. How did WinMain() happen? How did you get a console in the first place? With the rough guess that you didn't get one, so can't see any output either. Try again with the proper project template, in later VS2017 versions it is Visual C++ > Windows Desktop > Windows Console Application. – Hans Passant May 24 '18 at 15:35
  • I supposed I should clarify that this is code is a snippet from code that used to work 100% fine on the old VS that I am trying to get up and running on Win10/VS2017. – mpchenette May 24 '18 at 15:41
  • Surely you are missing the point? It is not supposed to work. – Hans Passant May 24 '18 at 15:42
  • Apologies, I guess I am missing the point, could you clarify? I get a console because I run the .exe from cmd prompt but when the program runs there is no output. – mpchenette May 24 '18 at 15:45
  • 1
    Well, not exactly, that console is owned by cmd.exe. Both it and your program is writing to it at the same time. Who will win is a crap-shoot. Win10 has big changes to the console plumbing because of the new Linux sub-system, probably has something to do with it as well. Consider running your program with start /wait yourapp.exe so no intermingling can occur. – Hans Passant May 24 '18 at 16:04
  • You created a GUI app. You meant to create a console app. – Raymond Chen May 24 '18 at 16:36

0 Answers0