0

So I have program that transforms data by piping the string into stdin like so.

cat input.mp3 | myexe

except I want this commandline tool to run on Windows.

type input.mp3 | myexe.exe

and cat and type provide different inputs because of how Windows treat binary files according to the question that referred to me to use cat. So how can I have a more a faithful representation of cat on Windows? Or is there a way I can the input from cat in pure C? Any help would be appreciated.

Update: Here is my code https://github.com/Skylion007/LVDOWin

forked and ported from: https://github.com/m13253/lvdo

I've modified the argument parsing slightly in my own code (only get-opt not get-opt-long), other than that I am using the same command line statements of the readme in the later repo. I would post the code here, but there is a lot a fluf and I don't want overwhelm an already complex answer.

type hello.mp3 | lvdoenc.exe -s 640x480 -q 6 -b8 | x264 --input-res 640x480 --fps 1 --profile high --level 5.1 --tune    stillimage --crf 22 --colormatrix bt709 --me dia --merange 0 -o public.mkv -

is an example of a command I used to encode the file initially. On Linux, I would use cat.

Skylion
  • 2,696
  • 26
  • 50
  • 1
    Take a look at unxutils (http://unxutils.sourceforge.net/) for Windows versions of cat and other utilities. – jarmod Jun 21 '15 at 19:31

1 Answers1

3

You can just redirect input from the file this way:

myexe < input.mp3

and force stdin to binary mode at the beginning of the main function in myexe this way:

_setmode(_fileno(stdin), O_BINARY);

You may need to add or remove underscores depending on your tools version.

The text mode versus binary mode is a Windows oddity inherited from the dark ages of microcomputing. It goes back all the way to the 1970s, from a popular pre-MSDOS operating system called CP/M from which is was copied. Unix and all its variants never had this and Apple ditched its non-standard end of line scheme a long time ago. You are unlikely to run you code on old mainframes which had their own weird notion on line based text files.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • How do I set stdin to binary mode isn't that done in a header file? Also, would that affect the deployment of this code on other operating systems? – Skylion Jun 21 '15 at 19:31
  • Update, setting the mode to binary and using the < does not seem to work. It still only encodes the header format – Skylion Jun 21 '15 at 19:33
  • What to you mean by *encodes the header format*? Post your code, the intended behaviour and the unexpected results. – chqrlie Jun 21 '15 at 19:40
  • 1
    @Skylion, how do you output the result? If it's to `stdout` and should be binary, you'll also need `_setmode(_fileno(stdout), O_BINARY)`. If you're outputting to a file, is it open in binary mode, e.g. `"wb"`? – Eryk Sun Jun 21 '15 at 20:00
  • @eryksun Hmm, I am not sure GCC even will allow me to set the mode? I am using GCC to cross compile this tool from Linux so how should I do this? Can I do so using only the MACRO? Would it even recognize the proper MACRO commands? – Skylion Jun 21 '15 at 20:24
  • @Skylion: not gcc related, its a header file / C library issue. – chqrlie Jun 21 '15 at 21:36
  • @Skylion: I looked at your code, where do you set the mode for `stdin` and `stdout` to binary? – chqrlie Jun 21 '15 at 21:40
  • @chqrlie Sorry, I haven't pushed those changes yet. Otherwise, the code is the same. I will try to set it outside the MACRO using the other question I asked and see if that helps. – Skylion Jun 22 '15 at 19:18