【发布时间】:2013-07-11 02:26:12
【问题描述】:
我正在使用测试硬盘的工具 fstest.exe。它从命令行运行良好,显示执行各种文件创建/删除/修改任务需要多长时间。从命令行以fstest.exe otherParams 运行时,通常的输出如下所示:
---
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)
---
问题是当我将输出重定向到文件时,它不显示任何内容:
fstest.exe otherParams > out.txt 创建一个空的 out.txt 文件,即使该命令执行得很好(并在其执行过程中创建了一些测试文件)。
如何强制此应用程序将输出重定向到文件?我尝试使用 PowerShell(通过 Start-Process)更仔细地查看它,以及标准输出和标准输出错误流只是空的。
我尝试过的其他事情:
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,都是空的。)
什么会导致应用程序根据它是否被重定向而改变其输出,有什么方法可以让它重定向到文件?
更新:我已经掌握了源代码。它是 C++,输出只是简单的 printf 语句。
【问题讨论】:
-
您的程序是否正常退出(通过返回)?或者你用信号中断来打断它?可能是缓冲区没有被刷新或以这种方式退出。
-
@Gray 它总是正常退出(通过返回,退出代码为 0),但由于某种原因,它不会在重定向到文件时刷新缓冲区——仅在打印到控制台时。