简体   繁体   English

如何将程序的控制台输出存储在文本文件中?

[英]How to store the console output of a program in a text file?

Okay, so my gui program depends on another third-party console program to display info on pdfs. 好的,所以我的gui程序依赖于另一个第三方控制台程序在pdf上显示信息。 The console program takes the filename of the pdf as an argument and displays the info. 控制台程序将pdf的文件名作为参数并显示信息。 I store the displayed info in a textfile. 我将显示的信息存储在文本文件中。 My gui program then reads the text file and displays it in an edit window. 然后,我的gui程序将读取文本文件并将其显示在编辑窗口中。 For storing the displayed info in a text file , right now I am using the system call: 要将显示的信息存储在文本文件中,现在我正在使用系统调用:

 infodisplayer filename.pdf >> info.txt

Which stores the output into "info.txt" which my gui program then reads. 它将输出存储到我的gui程序随后读取的“ info.txt”中。 Now this displays an irritating console window because it needs a command processor. 现在,这将显示一个令人烦恼的控制台窗口,因为它需要命令处理器。 I want to not display the console window. 我不想显示控制台窗口。 So is there some way using the WinApi , Glib , Gtk+ or the C Standard Library that stores the output of a console program in a text file so that I won't have to resort to a system call? 那么,有没有办法使用WinApi,Glib,Gtk +或C标准库将控制台程序的输出存储在文本文件中,这样我就不必诉诸系统调用了? Thanks. 谢谢。

Sorry I know the I didn't describe my problem well, but what I am doing is this: 对不起,我知道我没有很好地描述我的问题,但是我正在做的是这样的: 逻辑

Follwing program should do the trick. 跟随程序应该可以解决问题。 BTW, this program uses Windows API. 顺便说一句,该程序使用Windows API。

HWND hWnd = FindWindow(null, "Console Window title here");

if (hWnd != NULL)
 {
      ShowWindow(hWnd, 0); // 0 = SW_HIDE               
 }

You can put this code block in a Timer event to check the existence of Console Window frequently. 您可以将此代码块放入Timer事件中,以经常检查控制台窗口的存在。 or even better you can use this: 甚至更好,您可以使用以下方法:

char MyCommand[]="cmd.exe /c infodisplayer filename.pdf >> info.txt"; 
int res = CreateProcess(NULL, MyCommand , NULL, NULL, FALSE, CREATE_NO_WINDOW ,
                        NULL, NULL, &StartInfo, &ProcInfo);
if (res)
{

   WaitForSingleObject(ProcInfo.hThread, INFINITE);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM