0

How can i redirect my input and output to a file?

Example if i execute my program as :-

abc.exe < input.txt > output.txt

It should read input from input.txt and write output to output.txt

Thanks in advance :)

3
  • 5
    The answer is in the question, so... what is being asked here? Commented Jan 29, 2011 at 6:31
  • @BlueRaja - Danny Pflughoeft :- I didn't know it can be done in c#. I have Linux little bit and so i was searching for the same thing in C#. Didn't know it is available by default ;). Commented Jan 29, 2011 at 7:01
  • Cheng: This question has nothing to do with C#. This is just how input redirection works, in both Windows and Linux (it's handled by the OS). See microsoft.com/resources/documentation/windows/xp/all/proddocs/… Commented Jan 31, 2011 at 16:00

3 Answers 3

3

you mean this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            String s;
            while ((s = Console.In.ReadLine())!= null)
            {
                Console.Out.WriteLine(s);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was using Console.WriteLine and Console.ReadLine
0

Calling a console application with the < and > will redirect input and output. Just use Console.Read or ReadLine and Write, WriteLine etc. in your program.

Comments

0

System.Console.SetIn(TextReader newIn)

System.Console.SetOut(TextWriter newOut)

Would be good if you want to vary outputs during program's run.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.