0

I have a program that takes an input from the user (dnaInput). Then I have a Console output that prints a DNA strand. I want this console output to be saved to the dnaInput string to update it.

Here is my code:

namespace ConsoleApp2
{
    class Program
    {
        public static string dnaInput;

        public static void Main(string[] args)
        {

            // Variables

            bool showMenu = true; // Boolean variable for the main menu (for while loop)

            while (showMenu) // Main menu loop, this makes the program go back to the main menu after any operation
            {
                showMenu = MainMenu();
            }

        }

        private static bool MainMenu()
        {
            Console.Clear();
            Console.WriteLine("Loaded Main Strand: "+dnaInput+"\n"); // *** IMPORTANT *** THIS SHOWS THE LATEST UPDATE OF THE MAIN DNA STRAND. THIS IS WHAT WE ARE TRYING TO FIX.
            Console.WriteLine(@"----Life on Mars---- DEBUG FOR MAIN STRAND" + "\n");
            Console.WriteLine("Choose an option:");
            Console.WriteLine("1) Input DNA Strand 1 by keyboard");
            Console.WriteLine("1) Input DNA Strand 2 by keyboard");
            Console.WriteLine("0) Exit");
            Console.Write("\r\nSelect an option: ");


            switch (Console.ReadLine())
            {

                case "1":
                    FirstCodon();
                    return true;

                case "2":
                    SecondCodon();
                    return true;

                case "0":
                    return false;

                default:
                    return true;
            }
        }

        public static void FirstCodon() // this runs when you select 1 on the main menu
        {
            Console.WriteLine("Input DNA Strand 1: ");
            dnaInput = Console.ReadLine();

        }
   
        public static void SecondCodon() // this runs when you select 2 on the main menu
        {
            Console.Write("save this to the string -> dnaInput");
            // what to do after this?
        }
    }
}

I tried StreamWriter to send the console output to a text file but this didn't work. I tried this:

FileStream filestream = new FileStream("out.txt", FileMode.Create);
var streamwriter = new StreamWriter(filestream);
streamwriter.AutoFlush = true;
Console.SetOut(streamwriter);
Console.SetError(streamwriter);

Instead of taking the console output I want, it took the console output of my main menu.

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 27 '22 at 15:45

1 Answers1

0

First Console.SetOut then Console.Write("save this to the string -> dnaInput");

private static void SecondCodon() // this runs when you select 2 on the main menu
    {
        var oldOut = Console.Out;

        using var fileStream = new FileStream("out.txt", FileMode.Create);
        using var streamWriter = new StreamWriter(fileStream);
        Console.SetOut(streamWriter);
        Console.SetError(streamWriter);

        Console.Write("save this to the string -> dnaInput");

        Console.SetOut(oldOut);
        Console.SetError(oldOut);
    }

Look also at this answers: Mirroring console output to a file

Viachaslau S
  • 106
  • 1
  • 5