输出显示在控制台中,但重定向到文件时消失 [英] Output shows up in console, but disappears when redirected to file

查看:458
本文介绍了输出显示在控制台中,但重定向到文件时消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的工具,测试硬盘,fstest.exe。它从命令行运行良好,显示执行各种文件创建/删除/ - 转换任务花费了多长时间。通常的输出,当从命令行运行 fstest.exe otherParams ,看起来像这样:

I'm using a tool that tests hard disks, fstest.exe. It runs fine from the command line, displaying how long it took to do various file-creation/-deletion/-mangling tasks. The usual output, when run from the command line as fstest.exe otherParams, looks like this:

---
CPU Usage: 0.0%
Disk reads/sec: 0
Disk writes/sec: 0
Disk bytes/read: 0
Disk bytes/write: 0
Test duration: 0 milliseconds, 1153 ticks (3507177 ticks/sec)
---

麻烦的是,当我将输出重定向到文件时,它不显示任何内容:

The trouble is that when I redirect the output to file, it doesn't display anything:

fstest.exe otherParams> out.txt 创建一个空的out.txt文件,即使该命令以其他方式执行也很好(并且作为其执行的一部分创建了几个测试文件)。

fstest.exe otherParams > out.txt creates an empty out.txt file, even though the command otherwise executed just fine (and created a few test-files as part of its execution).

如何强制此应用程序将输出重定向到文件?我已经尝试过使用PowerShell ),并且标准输出和标准错误流都是空的。

How can I force this application to redirect output to a file? I've tried looking at it more closely with PowerShell (via Start-Process), and both the standard-out and standard-error streams are just empty.

我尝试过的其他事情:

cmd /c "fstest.exe otherParams > out.txt"

fstest.exe otherParams 2>&1 >> out.txt

fstest.exe otherParams | sort

powershell Start-Process -FilePath .\fstest.exe -ArgumentList @("create2", "-openexisting") -RedirectStandardOutput out.txt -RedirectStandardError err.txt -wait

(同时创建out.txt和err.txt,均为空。)

(That creates both out.txt and err.txt, both empty.)

什么会导致应用程序根据是否重定向更改其输出,并且是否有任何方法可以将其重定向到文件?

What would cause an application to change its output depending on whether it's redirected, and is there any way I can make it redirect to file?

UPDATE :我已经掌握了源代码。它是C ++,输出是简单的 printf 语句。

UPDATE: I've gotten my hands on the source code. It's C++, and the output is just straightforward printf statements.

推荐答案

事实证明,在执行 printf 后,程序不会刷新stdout。显然,cmd打印到控制台时愿意刷新缓冲区,而不是在将输出重定向到文件时。 (或者可能文件句柄在控制台强制刷新之前关闭。)程序正常退出(通过返回,退出代码始终为0)。

It turns out the program in question wasn't flushing stdout after doing a printf to it. Apparently, cmd is willing to flush that buffer when printing to the console, but not when redirecting output to a file. (Or perhaps the file handle is closed before the console could force a flush.) The program was exiting normally (via return, exit code always 0).

问题:我不得不修复程序;没有什么我可以用命令行开关或重定向来改变它。

To answer the question: I had to fix the program; there was nothing I could do with command-line switches or redirects to change it.

这篇关于输出显示在控制台中,但重定向到文件时消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆