Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read binary data from stdin on Win32, and write it to file

What is the Windows (Win32) command-line equivalent of the Unix pipeline:

myprog myarg | cat >out.dat

Please note that I want to have a pipe, to test that myprog can write successfully to a pipe, so please don't simplify it to this:

myprog myarg >out.dat

I'd guess something like

myprog myarg | copy /b con out.dat

would work, but I don't have a Windows machine to check.

Please note that the generated data is binary, it contains all possible bytes values 0 .. 255, and all of them must be preserved intact, without any transformation.

like image 930
pts Avatar asked Jan 09 '14 21:01

pts


2 Answers

Since Windows doesn't come with such a program, here's a quick one in C.

#include <stdio.h>
#include <io.h>
#include <fcntl.h>

int main()
{
    char buffer[16384];
    int count;
    _setmode(_fileno(stdin), _O_BINARY);
    _setmode(_fileno(stdout), _O_BINARY);
    while ((count = fread(buffer, 1, sizeof(buffer), stdin)) != 0)
        fwrite(buffer, 1, count, stdout);
    return 0;
}

You can easily modify it to write to a file of your choosing instead of stdout.

like image 119
Mark Ransom Avatar answered Oct 06 '22 06:10

Mark Ransom


There's nothing built into Windows equivalent to the way you're using cat in the example given. The command you suggest (copy /b con) certainly won't work, because con is the console device, not the standard input.

You could try the GNU utilities for Win32, which includes a port of cat. Otherwise you may need to write your own code, which would of course be simple enough.

like image 28
Harry Johnston Avatar answered Oct 06 '22 05:10

Harry Johnston