【问题标题】:In Powershell, how do you redirect a binary file to standard in?在 Powershell 中,如何将二进制文件重定向到标准输入?
【发布时间】:2020-06-23 19:06:52
【问题描述】:

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

【问题讨论】:

  • 来自文档:“读取和写入二进制文件时,Encoding 动态参数使用 Byte 值,ReadCount 参数使用 0 值。”这会有帮助吗?
  • 是的,我看到了,但我不想将整个文件内容加载到内存中,因为将来二进制文件可能会很大。我暂时试试。我猜我的用例不符合在对象上操作而不是在蒸汽上操作的 powershell 理念。
  • 不要将高级 cmdlet 用于低级内容,而是使用 .NET 二进制读取器/写入器并直接访问 std 流,请参阅 Output binary data on powershell pipeline
  • 那篇文章是关于输出端的。我对输入感到困惑,因为 powershell 不支持“
  • @joseph Get-Content 读取行并省略 CR/LF 字节。这适用于文本文件,因为结果将是一个字符串数组,每个字符串代表一行。要管道原始数据(在这种情况下是二进制数据),请将-Raw 开关添加到Get-Content

标签: powershell


【解决方案1】:

如果添加-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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-25
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多