将 Visual C++ 控制台输出写入或复制到文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21697064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:47:03  来源:igfitidea点击:

Writing or Copying Visual C++ console output to text file

c++visual-c++visual-studio-2012includeperceptual-sdk

提问by Kandarp Joshi

I am working with Intel Perceptual Computing SDK Voice Recognition Module. Using Microsoft Visual Studio 2012 Professional, the SDK sample perceives the dictation and after processing the voice input prints it on the console window. All i want to do is to copy the output printed on console window and write it in a .txtfile. I am following the general way but somehow the text written in file is just some numbers.

我正在使用英特尔感知计算 SDK 语音识别模块。使用 Microsoft Visual Studio 2012 Professional,SDK 示例感知听写,并在处理语音输入后将其打印在控制台窗口上。我想要做的就是复制控制台窗口上打印的输出并将其写入.txt文件。我遵循一般方式,但不知何故写入文件的文本只是一些数字。

// Callback for recognized commands and alerts
class MyHandler: public PXCVoiceRecognition::Recognition::Handler, public    PXCVoiceRecognition::Alert::Handler 

{ public: MyHandler(std::vector &commands) { this->commands=commands; }

{ public: MyHandler(std::vector &commands) { this->commands=commands; }

virtual void PXCAPI OnRecognized(PXCVoiceRecognition::Recognition *cmd) 
{
  wprintf_s(L"\nRecognized: <%s>\n", (cmd->label>=0)?commands[cmd->label]:cmd-    >dictation); //this line prints the dictated statement//
  // writing to a text file
  printf("Writing to the txt file...");
  std::ofstream out("c:\MyVoice.txt");
  out<<cmd->dictation;
}

protected:
std::vector<pxcCHAR*> commands;

};

};

int wmain(int argc, wchar_t* argv[]) {
// Create session
PXCSmartPtr<PXCSession> session;
pxcStatus sts = PXCSession_Create(&session);
if (sts < PXC_STATUS_NO_ERROR) {
wprintf_s(L"Failed to create the PXCSession\n");
return 3;
}
// Parse command line
UtilCmdLine cmdl(session);
if (!cmdl.Parse(L"-file-iuid-grammar-sdname-realtime-eos",argc, argv)) return 1;

// Create PXCVoiceRecognition instance
PXCSmartPtr<PXCVoiceRecognition> vc;
sts=session->CreateImpl(cmdl.m_iuid, PXCVoiceRecognition::CUID, (void **)&vc);
if (sts<PXC_STATUS_NO_ERROR) 
{
 wprintf_s(L"Failed to create PXCVoiceRecognition\n");
 return 3;
}

// Find and initilize capture module
UtilCaptureFile capture(session,cmdl.m_recordedFile,false);
if (cmdl.m_sdname) capture.SetFilter(cmdl.m_sdname);

 // Query PXCVoiceRecognition profile
 PXCVoiceRecognition::ProfileInfo profile;
 for (int i=0;;i++) 
 {
  sts=vc->QueryProfile(i, &profile);
  sts=capture.LocateStreams(&profile.inputs);
  return 3;
 }
if (cmdl.m_realtime >= 0) capture.SetRealtime(cmdl.m_realtime);
// Set PXCVoiceRecognition profile
if (cmdl.m_eos) profile.endOfSentence = cmdl.m_eos;
sts=vc->SetProfile(&profile);

// Grammar intialization
pxcUID grammar = 0;
if (cmdl.m_grammar.size()<0)
{
  wprintf_s(L"Dictation Mode\n");
}

vc->SetGrammar(grammar);  
// SubscribeRecognition
MyHandler handler(cmdl.m_grammar);
vc->SubscribeRecognition(80, &handler); 
vc->SubscribeAlert(&handler);

// Processing loop
PXCSmartPtr<PXCAudio> audio;
PXCSmartSPArray sps(3);
wprintf_s(L"Press any key to exit\n");

while (!_kbhit()) 
{
  sts = capture.ReadStreamAsync(audio.ReleaseRef(),sps.ReleaseRef(0));
  sts=vc->ProcessAudioAsync(audio,sps.ReleaseRef(1));
  sps.SynchronizeEx();
}

}

}

回答by Rastaban

You can use >to redirect the output of the command run by Visual Studio. Add it to the command arguments by selecting your project in the solution explorer and clicking PROJECT->Properties->Configuration Properties->Debugging. Then enter > output.txtinto the Command Arguments. After you run your application, the file will appear in the Working Directory - which by default is the same directory as your .sln files.

可用于>重定向 Visual Studio 运行的命令的输出。通过在解决方案资源管理器中选择您的项目并单击 PROJECT->Properties->Configuration Properties->Debugging,将其添加到命令参数中。然后进入> output.txt命令参数。运行应用程序后,该文件将出现在工作目录中 - 默认情况下,该目录与 .sln 文件位于同一目录中。

回答by ChileAddict - Intel

I think he is simply suggesting to run your executable and direct it to a file:
C:\.exe > C:\output.txt

我认为他只是建议运行您的可执行文件并将其定向到一个文件:
C:\.exe > C:\output.txt

Here is a link about redirection.

这是一个关于重定向的链接。

回答by neverhoodboy

Generally speaking, if you could produce the output correctly in the console window, then you can just run the executable with a command line argument like > c:\result.txtto redirect the standard output to a file (I'm assuming you are on Windows since you have the visual-c++ tag), thus you don't need to change your code at all (everything you put to coutwill be written to the file specified in the argument). If you are using the Visual Studio GUI, command line arguments can be specified in the project's properties page, otherwise you could just type the command in a console window by hand.

一般来说,如果您可以在控制台窗口中正确生成输出,那么您可以使用命令行参数运行可执行文件,例如> c:\result.txt将标准输出重定向到文件(我假设您在 Windows 上,因为您有-c++ 标记),因此您根本不需要更改代码(您放入的所有内容都cout将写入参数中指定的文件)。如果您使用的是 Visual Studio GUI,则可以在项目的属性页面中指定命令行参数,否则您只需在控制台窗口中手动键入命令。