How to save output redirect to a file?

157 views Asked by At

New to C# and want to implement below reading from input.txt and write result to output.txt

myprogram.exe < input.txt > output.txt

my code as below

class Program
    {
        static void Main(string[] args)
        {
            String line = Console.ReadLine();
            while(line != null)
            {
                Console.Out.WriteLine(line);
                line = Console.ReadLine();
            }
        }
    }

The result is reading from input.txt is fine, and output.txt can be created but nothing in it.

UPDATE: the rootcause is output.txt been locked by other process which is not C# language related.

2

There are 2 answers

1
Chelonogov Alexander On
using System;
using System.IO;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            var parameters = GetParameters(args);

            var inputContent = GetInputContent(parameters);

            CreateOutputFile(parameters, inputContent);

            Console.WriteLine("it's ok. Press key to exit");
            Console.ReadKey();
        }

        private static void CreateOutputFile(MyProgramParameters parameters, string inputContent)
        {
            File.WriteAllText(parameters.OutputFileName, inputContent);
        }

        private static string GetInputContent(MyProgramParameters parameters)
        {
            var result = File.ReadAllText(parameters.InputFileName);

            return result;
        }

        private static MyProgramParameters GetParameters(string[] args)
        {
            CheckInputParameters(args);

            return new MyProgramParameters
            {
                InputFileName = args[0],
                OutputFileName = args[1]
            };
        }

        private static void CheckInputParameters(string[] args)
        {
            if (args.Length != 2)
                throw new ArgumentException("invalid parameters. For example, myProgram.exe input1.txt   output.txt");

            var inputFileName = args[0];

            if (!File.Exists(inputFileName))
                throw new IOException(string.Format("File {0} not found", inputFileName));
        }
    }

    public class MyProgramParameters
    {
        public string InputFileName { get; set; }

        public string OutputFileName { get; set; }
    }
}
1
Aleksa Ristic On

You haven't read documentation at all. Here you have the link: click

You should just do it in 2 steps:

  • Read from file to string[]
  • Write to file from string[]

To read use

string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");

To write use

System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);

Whole function would look something like this:

private void CopyFromFileToFile(string inputFileDest, string outputFileDest)
{
    string[] lines;
    lines = System.IO.File.ReadAllLines(inputFileDest);
    System.IO.File.WriteAllLines(outputFileDest, lines);
}