这不适用于二进制文件:Redirecting standard input\output in Windows Powershell
以下是我用于输入的内容的摘要。您可以看到 ls 显示文件大小为 858320 字节,但是当通过 Get-Content 进行管道传输时,情况并非如此。
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
Run Code Online (Sandbox Code Playgroud)
这是我的最小测试程序,它计算标准输入中的字节数,直到达到 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;
}
Run Code Online (Sandbox Code Playgroud)
这是输出。请注意,字节与 ls 不匹配。
PS C:\Users\Joseph\Documents\GitHub\java_hprof_to_txt> Get-Content .\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 860183 bytes
Run Code Online (Sandbox Code Playgroud)
我什至尝试过 -Encoding Byte 和 -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
Run Code Online (Sandbox Code Playgroud)
当我在普通命令终端中运行它时,它工作正常:
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
Run Code Online (Sandbox Code Playgroud)
如果添加参数-Raw
没有帮助:
Get-Content .\generate_hprof\heap.dump.hprof -Raw | .\x64\Debug\CountBytes.exe
Run Code Online (Sandbox Code Playgroud)
使用Start-Process
cmdlet 肯定会起作用:
Start-Process .\x64\Debug\CountBytes.exe -RedirectStandardInput .\generate_hprof\heap.dump.hprof
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2144 次 |
最近记录: |