TCM
TCM

Reputation: 16910

How do i redirect input output to file instead of stdout?

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 :)

Upvotes: 0

Views: 1023

Answers (3)

Andrey Taptunov
Andrey Taptunov

Reputation: 9515

System.Console.SetIn(TextReader newIn)

System.Console.SetOut(TextWriter newOut)

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

Upvotes: 0

matthias.lukaszek
matthias.lukaszek

Reputation: 2220

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.

Upvotes: 0

Fredou
Fredou

Reputation: 20140

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);
            }
        }
    }
}

Upvotes: 3

Related Questions