Anthony
Anthony

Reputation: 61

How to save output to text file

I've made a program where I can decrypt some encrypted text, it does this by reading a text file and shifting the text one letter at a time, and it does this 26 times, but I can't get it to save and write the output to a text file, this is my code below, I would appreciate any help if possible, thank you

using System;
using System.IO;

namespace Assignment1
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            //Writing to the screen
            Console.WriteLine("Welcome to the Ceaser Cipher Shift Program");
            Console.WriteLine("Would You Like to Decrypt a File (y for yes/n for no)");

            string userValue = Console.ReadLine();

            //User's value is set to userValue
            // if the user types "y", activate the decryption method
            if (userValue.ToLower() == "y")
            {
                decryption();
            }

            // if the user types in "n", write "Goodbye" and close the program
            if (userValue.ToLower() == "n")
            {
                Console.WriteLine("Goodbye");
                Console.ReadLine();
                Environment.Exit(0);
                //Environment.exit closes the program
            }
        }

        //Decryption method
        public static void decryption()
        {
            //ShiftLine is equal to new char
            char[] ShiftLine = new char[0];

            //If the shift is smaller or equal to 25, continue to shift one at a time
            for (int shift = 1; shift <= 26; shift++)
            {
                //This reads the encryptefd text file into the program
                string textFile = @"C:\Users\Anthony\Desktop\caesarShiftEncoded.txt";
                //The string "test" reads all the lines of the textFile
                string[] text = File.ReadAllLines(textFile);

                foreach (string line in text)
                {
                    //Sets currentLetter to 0
                    int CurrentLetter = 0;
                    int[] ShiftNumbers = new int[line.Length];
                    ShiftLine = new char[line.Length];

                    foreach (char letter in line)
                    {
                        ShiftNumbers[CurrentLetter] = ConvertLetterToNumber(letter);
                        ShiftNumbers[CurrentLetter] = ShiftCipher(ShiftNumbers[CurrentLetter], shift);
                        ShiftLine[CurrentLetter] = ConvertNumberToLetter(ShiftNumbers[CurrentLetter]);
                        CurrentLetter++;
                    }

                    Console.WriteLine(string.Join("", ShiftLine));
                }

                //Console.WriteLine (textFile.Length);
                Console.WriteLine("This is Shift No: {0}", shift);
            }

            Console.WriteLine("Which Shift Would You Like To Write to a Text File: ");
            string userNumber = Console.ReadLine();

            if (userNumber.ToLower() == "1 =< 26")
            {
                using (StreamWriter text = new StreamWriter("TextWrittenFile.txt"))
                {
                    foreach (char line in ShiftLine)
                    {
                       text.WriteLine(ShiftLine);
                       File.WriteAllText("C:\Users\Anthony\Desktop\DecryptedText.txt", ShiftLine);
                    }
                }
            }
        }

        public static int ShiftCipher(int Number, int Shift)
        {
            if (Number == 27)
            {
                return 27;
            }
            else if (Number == 28)
            {
                return 28;
            }
            else if (Number > Shift)
            {
                return (Number - Shift);
            }
            else if (Number <= Shift)
            {
                return (26 + (Number - Shift));
            }
            else
            {
                return 0;
            }
        }

        public static int ConvertLetterToNumber(char Letter)
        {
            switch (Char.ToLower(Letter))
            {
                case 'a':
                    return 1;
                case 'b':
                    return 2;
                case 'c':
                    return 3;
                case 'd':
                    return 4;
                case 'e':
                    return 5;
                case 'f':
                    return 6;
                case 'g':
                    return 7;
                case 'h':
                    return 8;
                case 'i':
                    return 9;
                case 'j':
                    return 10;
                case 'k':
                    return 11;
                case 'l':
                    return 12;
                case 'm':
                    return 13;
                case 'n':
                    return 14;
                case 'o':
                    return 15;
                case 'p':
                    return 16;
                case 'q':
                    return 17;
                case 'r':
                    return 18;
                case 's':
                    return 19;
                case 't':
                    return 20;
                case 'u':
                    return 21;
                case 'v':
                    return 22;
                case 'w':
                    return 23;
                case 'x':
                    return 24;
                case 'y':
                    return 25;
                case 'z':
                    return 26;
                case ' ':
                    return 27;
                default:
                    return 0;
            }
        }

        public static char ConvertNumberToLetter(int Number)
        {
            switch (Number)
            {
                case 1:
                    return 'a';
                case 2:
                    return 'b';
                case 3:
                    return 'c';
                case 4:
                    return 'd';
                case 5:
                    return 'e';
                case 6:
                    return 'f';
                case 7:
                    return 'g';
                case 8:
                    return 'h';
                case 9:
                    return 'i';
                case 10:
                    return 'j';
                case 11:
                    return 'k';
                case 12:
                    return 'l';
                case 13:
                    return 'm';
                case 14:
                    return 'n';
                case 15:
                    return 'o';
                case 16:
                    return 'p';
                case 17:
                    return 'q';
                case 18:
                    return 'r';
                case 19:
                    return 's';
                case 20:
                    return 't';
                case 21:
                    return 'u';
                case 22:
                    return 'v';
                case 23:
                    return 'w';
                case 24:
                    return 'x';
                case 25:
                    return 'y';
                case 26:
                    return 'z';
                case 27:
                    return ' ';
                default:
                    return '0';
            }
        }
    }
}

Upvotes: 0

Views: 924

Answers (2)

Markus Safar
Markus Safar

Reputation: 6580

Actually there are a few things you could improve. If the user shall only be able to press y or n, you could use the Console.ReadKey() method instead of letting the user input a complete line of text. By using the overloaded method and specifying true as argument, you won't even see the pressed key on written into the console window (Console.ReadKey(true)).

So you could exchange the current if statements with switch (Console.ReadKey(true).Key) and build cases like case ConsoleKey.Y:

So the Main method would look something like that:

public static void Main(string[] args)
{
    //Writing to the screen
    Console.WriteLine("Welcome to the Ceaser Cipher Shift Program");
    Console.WriteLine("Would You Like to Decrypt a File (y for yes/n for no)");

    switch (Console.ReadKey(true).Key)
    {
        // if the user types "y", activate the decryption method
        case ConsoleKey.Y:
            decryption();
            break;

        // if the user types in "n", write "Goodbye" and close the program
        case ConsoleKey.N:
        default:
            Console.WriteLine("Goodbye");
            Environment.Exit(0);
            break;
    }
}

I guess the next thing you want to do is using Caesar cipher for encrypting/decrypting data.

Therefore you "just" need to shift every character and this can be done much easier than writing switch-case statements like you did. The idea behind this type of encryption is, that you take one character and replace it with another one which is offset characters away. Decrypting goes the other way.

So it would be much easier building one method that has the ability to do exactly that for a specified text.

There are two special cases you need to consider: What happens if the value you are looking at is on the lower boundary and you substract the offset (meaning, the "new" value would be lower than zero). The second one represents the same situation on the upper boundary.

These sitations can be resolved using either if statements or possibly easier by just looking at the rest of the division with the maximum value range. (text[i] + offset) % NumberOfPossibleCharacters should do the trick.

Knowing this you can easily build a method like the following one (where I have assumed, that there is no character with a value greater than 127):

static string Encrypt(string data, int offset)
{
    string result = string.Empty;

    for (int i = 0; i < data.Length; i++)
    {
        // Add the offset to the current character and just take the
        // rest of the division by 127. Afterwards cast it back to a character
        // (because it will be a number due to + and %)
        result += (char)((data[i] + offset) % 127);
    }

    return result;
}

Be advised, that string operations like += will cause the creation of a new string each time so it is not a good example from an (memory)performance point of view. There is the better way of using the StringBuilder class.

I hope this hint's you in a better way of solving your homework ;-)

Upvotes: 1

Gottfried Lesigang
Gottfried Lesigang

Reputation: 67281

As your ShiftLine seems to be an array of chars it could be as simple as that:

File.WriteAllText("C:\Users\Anthony\Desktop\DecryptedText.txt", new string(ShiftLine));

(instead of your loop [btw: Your loop overwrites the file on any turn...])

Upvotes: 3

Related Questions