【问题标题】:Writing or Copying Visual C++ console output to text file将 Visual C++ 控制台输出写入或复制到文本文件
【发布时间】:2014-03-08 22:57:14
【问题描述】:

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

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

{ 民众: MyHandler(std::vector &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();
}

}

【问题讨论】:

  • 能否正确格式化您的代码
  • @Sean 我在以这种方式格式化它时遇到了很大的麻烦。它不允许我发布更改。

标签: c++ visual-c++ visual-studio-2012 include perceptual-sdk


【解决方案1】:

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

【讨论】:

  • 谢谢。这适用于普通应用程序。不知何故,当我对上面发布的这段语音识别代码做同样的事情时,控制台没有给出任何东西。如果没有这些更改,它可以正常工作并为我提供输出。但除了制作一个空的 txt 文件外,对 > output.txt 没有任何作用。
  • 您的输出可能会发送到 stderr 而不是 stdout?要捕获标准错误,请使用 2&gt; output.txt 而不是 &gt;。您也可以使用&gt; output.txt 2&gt;&amp;1 将两者合并到同一个文件中
  • 好。但有一个转折点。该程序正在完美执行并制作一个txt文件。但它不会将控制台输出写入 txt 文件。
  • (如果这有帮助)我想我的输出是 unicode 流,因为如果我使用 wprinf_s,它只会在控制台上打印。它不适用于 printf 或 printf_s。 @拉斯塔班
【解决方案2】:

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

这是一个关于redirection的链接。

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 是的,我使用的是 windows 7。你能给我一个链接,详细说明你刚才描述的方法吗?
    猜你喜欢
    • 2012-02-01
    • 2012-12-18
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多