在Powershell中,如何将二进制文件重定向到中的标准?
此选项不适用于二进制文件: 下面是我使用的输入的摘要。您可以看到ls显示的文件是858320字节,但是当通过管道获取内容时,情况并非如此在Powershell中,如何将二进制文件重定向到中的标准?,powershell,Powershell,此选项不适用于二进制文件: 下面是我使用的输入的摘要。您可以看到ls显示的文件是858320字节,但是当通过管道获取内容时,情况并非如此 PS C:\Users\Joseph\Documents\GitHub\java_hprof_to_txt> ls .\generate_hprof\heap.dump.hprof Directory: C:\Users\Joseph\Documents\GitHub\java_hprof_to_txt\generate_hprof Mo
PS C:\Users\Joseph\Documents\GitHub\java_hprof_to_txt> ls .\generate_hprof\heap.dump.hprof
Directory: C:\Users\Joseph\Documents\GitHub\java_hprof_to_txt\generate_hprof
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 1/28/2017 12:02 PM 858320 heap.dump.hprof
这是我的最小测试程序,它从标准输入计算字节数,直到到达eof:
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main()
{
char buff;
int numOfBytesRead;
int dataLen = 0;
#ifdef _WIN32
int result = _setmode(_fileno(stdin), _O_BINARY);
if (result == -1)
perror("Cannot set mode");
else
printf("'stdin' successfully changed to binary mode\n");
#endif
while (true) {
numOfBytesRead = fread(&buff, 1, 1, stdin);
if (numOfBytesRead != 1) {
if (feof(stdin)) {
fprintf(stdout, "end of file reached\n");
}
int errorCode = ferror(stdin);
if (errorCode) {
fprintf(stdout, "error reading file %d\n", errorCode);
}
perror("The following error occurred:");
break;
}
dataLen++;
}
fprintf(stdout, "read %d bytes\n", dataLen);
return 0;
}
我甚至尝试了-Encoding字节和-Encoding Unknown,但这没有帮助:
PS C:\Users\Joseph\Documents\GitHub\java_hprof_to_txt> Get-Content -Encoding Byte .\generate_hprof\heap.dump.hprof | .\x64\Debug\CountBytes.exe
'stdin' successfully changed to binary mode
end of file reached
The following error occurred:: No error
read 3253650 bytes
PS C:\Users\Joseph\Documents\GitHub\java_hprof_to_txt> Get-Content -Encoding Unknown .\generate_hprof\heap.dump.hprof | .\x64\Debug\CountBytes.exe
'stdin' successfully changed to binary mode
end of file reached
The following error occurred:: No error
read 429608 bytes
当我在普通命令终端中运行它时,它工作正常:
C:\Users\Joseph\Documents\GitHub\java_hprof_to_txt>.\x64\Debug\CountBytes.exe < .\generate_hprof\heap.dump.hprof
'stdin' successfully changed to binary mode
end of file reached
The following error occurred:: No error
read 858320 bytes
C:\Users\Joseph\Documents\GitHub\java\u hprof\u to\u txt>\x64\Debug\CountBytes.exe<.\generate\u hprof\heap.dump.hprof
“stdin”已成功更改为二进制模式
已到达文件结尾
发生以下错误::无错误
读取858320字节
如果添加-Raw
参数没有帮助:
Get-Content .\generate_hprof\heap.dump.hprof -Raw | .\x64\Debug\CountBytes.exe
使用启动流程
cmdlet可以确保:
Start-Process .\x64\Debug\CountBytes.exe -RedirectStandardInput .\generate_hprof\heap.dump.hprof
在文档中:“当读取和写入二进制文件时,使用Byte值作为编码动态参数,使用0值作为ReadCount参数。”这会有帮助吗?是的,我看到了,但我不想将整个文件内容加载到内存中,因为将来二进制文件可能会很大。我现在就试试。我猜我的用例不符合powershell在对象上操作而不是在流上操作的理念。不要对低级内容使用高级cmdlet,而是使用.NET二进制读取器/写入器并直接访问std流,请看这篇文章是关于输出端的。我对输入感到困惑,因为powershell不支持“@joseph
Get Content
读取行并省略CR/LF字节。这适用于文本文件,因为结果将是一个字符串数组,每个字符串表示一行。通过管道传输原始数据(本例中为二进制数据),将-Raw
开关添加到获取内容
。
Start-Process .\x64\Debug\CountBytes.exe -RedirectStandardInput .\generate_hprof\heap.dump.hprof