4

I need to build an simple console application that accepts a pipe as input. We are running Windows Server 2012. The data is coming from another application that is going to "pipe" the input to this application. I have an understanding of pipes from a Linux perspective but do not understand them well from a Windows perspective.

My best guess is that I need to send input to my application like this: C:\app.exe < test.txt

When using the '<' character my current understanding is that it converts test.txt to a stream and will pass in a pointer.

My question is, can anyone give me an example of how to receive a stream pointer, or something equivalent to a pipe in windows in my application, so that I can read the input?

Payson Welch
  • 337
  • 4
  • 11
  • 1
    possible duplicate of [C# Console receive input with pipe](http://stackoverflow.com/questions/199528/c-sharp-console-receive-input-with-pipe) – Mike Perrenoud Mar 20 '13 at 14:02
  • `<` input redirection has nothing to do with pipes, streams and pointers. Do you really need to use pipes or in reality you need something else? It is not clear from the question. – wRAR Mar 20 '13 at 14:03
  • @MichaelPerrenoud I just found that post, and yes this works for both using a pipe '|' and the file input character '<' – Payson Welch Mar 20 '13 at 14:05
  • @PaysonWelch, so does that answer your question? – Mike Perrenoud Mar 20 '13 at 14:07

1 Answers1

5

When you use < and > with an application, the standard input and output streams (screen/keyboard interface) is replaced by a file stream.

You can use the regular Console.Read and Console.ReadLine commands to read from the stream specified by the < directive, or use Console.In which is a TextReader.

Similarly Console.Write and Console.WriteLine can be used to write to the output stream specified by the > directive, or Console.Out which is a TextWriter.

If you use the | pipe directive, for example myapp.exe | sort, the output stream of the first program goes into the input stream of the next program.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005