gpt4 book ai didi

powershell - 在 Powershell 中,如何将二进制文件重定向到标准中?

转载 作者:行者123 更新时间:2023-12-02 23:41:12 26 4
gpt4 key购买 nike

这个不适用于二进制文件: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

这是我的最小测试程序,它计算标准输入的字节数,直到到达 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;
}

这是输出。请注意,字节与 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

我什至尝试了 -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

当我在普通命令终端中运行它时,它工作正常:

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

最佳答案

如果添加 -Raw 参数没有帮助:

Get-Content .\generate_hprof\heap.dump.hprof -Raw | .\x64\Debug\CountBytes.exe

使用 Start-Process cmdlet 肯定有效:

Start-Process .\x64\Debug\CountBytes.exe -RedirectStandardInput .\generate_hprof\heap.dump.hprof

关于powershell - 在 Powershell 中,如何将二进制文件重定向到标准中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41995126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com