How do I tie into the standard input and output with C#? - c#

What I am trying to do is see if I can use the MinGW C++ compiler and debugger to compile files using a C# app. I want to be able to read the output and write to the input (input mainly for the debugger) within the C# app whenever I need to.
Reading from output will be much like Visual Studio does in its 'Output' window durring a project build.
I have used System.Diagnostics.Process before but haven't been able to figure out how to interact with the processes.
So, how do I take control of std input/output?

Redirecting standard input
Reading standard output

The following question shows how to capture standard output:
How do I run a Console Application, capture the output and display it in a Literal?
Writing to standard input is similar -- set RedirectStandardInput to true, and use a streamwriter to write to it:
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput.aspx

Related

What is standard input and output?

I'm starting to work on a chess engine and I want to follow the uci interface where comunication is done using plane text. In the specification it says
all communication is done via standard input and output with text commands
but I dont know what this means in real terms.
I'm thinking of writing in C# as a .net standard library. I understand that as uci is cross platform we cannot talk in language like Console.WriteLine but what does that line in the api mean for me.
Do I have to run a loop listening for Console.ReadLine() or something similar for standard input? I just don't get it. Or should I be writing a console application that takes the input as a command line argument, and writes string to the console?
The full specification can be downloaded from this link:
http://download.shredderchess.com/div/uci.zip
Any process have three default streams:
Standard input (stdin): This is the only input stream for all terminal or console aps. When you cal Console.ReadLine or Console.Read the result is taken from this stream.
Standard Output (stdout): When you call output-related commands in the console singleton class, all data goes here. For example the WriteLine or theWrite methods. The color and beeps commands send data to there too.
Standard error (stderr): This is a dedicated stream to print error-related content to the console. This is dedicated because some applications and scripting solutions want to hide these messages.
You can communicate with the containing process (such a console prompt) with these streams. The command line arguments can be passed only to the program's main method.

Redirecting C# Standard Output and Reading it with Python

I'm trying to redirect that standard output from a Command Line Project I wrote in C# and reading through the data in a Python file.
Currently, the C# application writes data that it reads from a sensor into a CSV file. I have to run the Python file later to get and process the data (has to be done in Python and the data collection has to be done in .NET to use the SDK).
I want to be able to run the C# and the Python projects at the same time and transfer the stream of data directly from the C# to Python project without the use of an intermediate, local file (the CSV).
I've done my own hunting on SO and in the MSDN Documentation. I'm looking at using the ProcessStartInfo.RedirectStandardOutput Property to redirect the console output of the C# application.
I don't yet know how to pick up on the data in this Stream from a Python file. This article was helpful in grasping a better understanding of how to approach it, but I'm still stuck.
I'm also looking at subprocess.Popen.communicate in Python but I'm not yet sure if that would work for what I am asking.
Any help would be appreciated.
Thanks to #Quantic for providing some great resources. Using subprocess.Popen, I can run the built .exe of my C# project and redirect the output to my Python file. I am able now to print() to output to the Python console all the information being output to the C# console (Console.WriteLine()).
Python code:
from subprocess import Popen, PIPE, STDOUT
p = Popen('ConsoleDataImporter.exe', stdout = PIPE, stderr = STDOUT, shell = True)
while True:
line = p.stdout.readline()
print(line)
if not line:
break
This gets the console output of the .NET project line by line as it is created and breaks out of the enclosing while loop upon the project's termination.

webpage which should take C/C++ program from textarea

I am new to ASP.NET and I have a challenge here:
I have to create a webpage which should take C/C++ program from textarea and should produce output of the program below the textarea using label when a button is clicked on the webpage.
I have no difficulty creating the front page(textarea,button,label or other controls) and reading the content of textarea in back end page(cs page).
But I don't have any idea how I can compile and run the program read from textarea and produce the output which can be assigned to a label on webpage.
Any help on this will be much appreciated.
Thanks
You can use
System.Diagnostics.Process.Start()
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx
followed by
Process.WaitForExit
to run the Visual C++ compiler (CL.EXE). The documentation for CL.EXE is here: http://msdn.microsoft.com/en-us/library/610ecb4h.aspx
Your needs might be as simple as:
c:\path-to-compiler\cl.exe file.cpp /clr
In particular, I'd recommend that you review all your choices for the /clr option: http://msdn.microsoft.com/en-us/library/k8d11d4s(v=VS.80).aspx
You need to have Visual C++ on your machine.
I assume you only want to treat single source file programs using only standard libraries. I also assume that you are working on Windows (asp.net). As soon as you have program source text, you can save it to a file. Then you start cl as an external program with your source as parameter to compile it. And finally you start the program you have just compiled. Of course, you will have to give pipes for Stdout and probably stdin and stderr.

Standard Input & Output in .NET

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.
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
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
You can't use Console.ReadLine() and Console.WriteLine()?
Never mind the above. Have you tried System.IO.Pipes? Here's the MSDN documentation.

How to write a vb.net code to compile C/C++ programs?

I'm trying to make a vb.net application that has got 2 textboxes, 7 radio buttons and 2 buttons(one named compile and the other 'run'). How can I load the content of a C/C++(or any programming language) file into the 1st textbox and on clicking the compile button, i should be able to show the errors or the C/C++ program in the 2nd textbox. On clicking Run, I should be able to show the output in the 2nd textbox. In short, I want to use the 2nd textbox as a terminal/console. The radio buttons are 4 selecting the language C or C++ or python or C# or java or perl or vb.
Are the compilers of all these languages present in .net? If so how can I call them?
Look at the System.IO namespace for clues as to how you go about loading the contents of a file into a text box. In particular, the File class.
System.IO.File Class
Look at the System.Diagnostics namespace for clues as to how to go about launching a process and capturing the output. In particular, the Process class.
System.Diagnostics.Process Class
This SO page...
Capturing the Console Output in .NET (C#)
... will give you some more info around capturing console output.
Compiling can be done by calling cl.exe which comes with Visual Studio. Of course you could also use GCC instead.

Categories

Resources