문제

I am writing a little program in c++ which creates an .exe which i then run by calling it with parameters in cmd. I want to be able to display output from the .exe into the cmd that I ran it from. I currently have this code which opens a new cmd window to display output which is close but not what i want. any help with this would be great! thanks.

AllocConsole();
DWORD NumberOfBytesWritten = 0;
WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), strLog1, lstrlen(strLog1), &NumberOfBytesWritten, 0);

Update: I have also been able to write to a text file using dir > log.txt in the command window when calling the program, is there a way I can change this so that it directs outputs to the console window? Thanks,

도움이 되었습니까?

해결책

My psychic debugging powers tell me that your build tools are configured to create your application in GUI rather than console mode.

If you reconfigure the build so that it generates a console mode application, you won't need to call AllocConsole or do anything special; you'll automatically be assigned to the console of the parent process.

다른 팁

Did you try simple operations, such as:

std::cout << "Print me" ;

or

std::cerr << "Print me too";

?

(I hope I understood correctly that you want to print to the same console where you started your app)

That question has already been asked: How to output to the console in C++/Windows. Here is an answer that seems to be useful in your case: https://stackoverflow.com/a/587792/1728537

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top