G
G
gvadelypa2020-09-05 15:51:38
C++ / C#
gvadelypa, 2020-09-05 15:51:38

How to read from input and output to output in C#?

What should the input from input.txt look like, and the output to output.txt, when solving Olympiad problems?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris the Animal, 2020-09-05
@NovichekTyrnira

using System;
using System.IO;

namespace InputOutputExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "input.txt");
            string outputFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "output.txt");

            using var input = new StreamReader(File.OpenRead(inputFile));
            Console.SetIn(input);
            
            using var output = new StreamWriter(File.OpenWrite(outputFile));
            Console.SetOut(output);

            string line = null;
            while ((line = Console.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
        }
    }
}

5f539743c3eec503656175.png
5f53974b97194787313157.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question