3

How can I read from standard input and write to standard output. System.Diagnostics.Process.StandardInput's MSDN reference didn't help as it separately starts the process and then redirects the Standard Input/Output but what If the process is already running and called my Application to feed it some data. Here's an example to make things a bit clear:

I am simply using Unix pipes i.e. cat command in cygwin (A Linux like Environment for windows) that basically just reads standard input and print to standard output. following is the command:

% cat input/sample.txt | src/csharp/maptest

But that doesn't seems to work.

If some one know ruby here as i don't here's what i want to do the same in C#:

#!/usr/bin/env ruby 

STDIN.each_line 
do |line| 
some code here 
end

And here's some python equivalent code that i want to accomplish in c# or vb.net:

#!/usr/bin/env python 

import re 
import sys 

for line in sys.stdin: 
val = line.strip() 

Any solutions?

Thanks in advance.

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
Ali
  • 1,648
  • 2
  • 26
  • 48

3 Answers3

9

You're looking for the static methods in the Console class :

  • System.Console.In encapsulate the standard input stream
  • System.Console.Out encapsulate the standard output stream
KeatsPeeks
  • 19,126
  • 5
  • 52
  • 83
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

Pointers to documentation for System.IO.Pipes are a red herring here.

If you just want the ability to have a process pipe its standard output to another process's standard input, then this works the same as you would expect provided the target process is written to read input from standard input. For example I can do

dir /b /s c:\*.* | findstr exe

to find all executable files on my C: drive.

All you need to do therefore is to build your maptest application so that it reads from standard input, in other words it must accept input via the Console.Read* methods as mentioned in other answers. See this previous question for an earlier discussion

C# Console receive input with pipe

Community
  • 1
  • 1
Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
0

You can't use Console.ReadLine() and Console.WriteLine()?

Never mind the above. Have you tried System.IO.Pipes? Here's the MSDN documentation.

  • I think he means starting another process from inside an application and accessing standard input/output for that process. – Nelson Rothermel Oct 29 '10 at 21:05
  • @Matthew No that doesn't seems to work either and same goes with Console.In and Console.Out. All I need is a way to read the standard input and write to standard output. – Ali Oct 29 '10 at 21:07
  • It's difficult to tell exactly, but I got the impression he meant that the program he's creating is going to be fed info from std::in – Andrew Barber Oct 29 '10 at 21:08
  • @Nelson Some application called my application and then my application should read data from standard input. – Ali Oct 29 '10 at 21:09
  • I need to clean my glasses. I should have realized that he said pipes. –  Oct 29 '10 at 21:10
  • If some one know ruby here as i don't here's what i want to do the same in C#: #!/usr/bin/env ruby STDIN.each_line do |line| some code here end – Ali Oct 29 '10 at 21:12
  • And here's some python equivalent code that i want to accomplish in c# or vb.net: #!/usr/bin/env python import re import sys for line in sys.stdin: val = line.strip() – Ali Oct 29 '10 at 21:16
  • Ali, have you had a look at the System.IO.Pipes documentation? –  Oct 29 '10 at 21:18
  • Matthew, I'm going through the documentation, hope it'll help. – Ali Oct 29 '10 at 21:20