在文件中保存循环输出仅保存最后一次迭代

时间:2015-12-02 14:22:48

标签: c# encryption

我正在创建一个凯撒密码解密/加密程序。我的程序从.txt文件中读取,显示26个不同班次的所有输出。

在将输出保存回.txt时遇到问题:只有最终迭代的输出才会保存到文件中。我希望保存所有26次迭代。

参见图片。

Console Output

.txt output

循环和写入文件的代码:

static void decryption()
    {   Console.ForegroundColor = ConsoleColor.DarkBlue;
        Console.WriteLine ("\n*********************************** Decryption *********************************");
        Console.ResetColor();
        //pulls getPath from varables class
        string path = globalVars.getPath();


    string encrypted_text = System.IO.File.ReadAllText(path);    //String variable that contains the text from a file. To get the text, the method in a class SystemIO is ran to read the text. It expects a parameter, which is a file directory.
    string decoded_text = " ";
    int shift = 0;
    char character = '0';
    encrypted_text = encrypted_text.ToUpper();

    char[] alphabet = new char[26] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

    Console.WriteLine("The encrypted text is \n{0}", encrypted_text);       //Display the encrypted text

    for (int i = 0; i < alphabet.Length; i++)        //Start a loop which will display 25 different candidates of decipher
    {
        decoded_text = "";
        foreach (char c in encrypted_text)
        {
            character = c;

            if (character == '\'' || character == ' ')
                continue;

            shift = Array.IndexOf(alphabet, character) - i;     //Define a shift which is the index of a character in an alphabet array, take away the itteration of this loop. Store the result in a variable
            if (shift <= 0)
                shift = shift + 26;

            if (shift >= 26)
                shift = shift - 26;


            decoded_text += alphabet[shift];
        }
        Console.WriteLine("\nShift {0} \n {1}", i + 1, decoded_text);
    }

        string filename;
        string savePath;

        string fileContent = decoded_text;

        Console.WriteLine("What do you want to name your file??");
        filename = Console.ReadLine();

        Console.WriteLine("Where would you like to save your file??");
        savePath = Console.ReadLine();

File.WriteAllText(savePath + filename + ".txt", fileContent);
        Console.WriteLine("Success");
        Console.WriteLine(Console.Read());
}
}// ///////END OF DECRYPTION //////////

3 个答案:

答案 0 :(得分:4)

问题在于这一行string fileContent = decoded_text; 您只是将fileContent分配给上次解码的文本。要解决此问题,请在循环之前输入fileContent,然后在每个步骤中添加每个decodeText。

像这样:

static void decryption()
{   

    // ...
    string fileContent = "";

    for (int i = 0; i < alphabet.Length; i++)
    {
        // ...
        fileContent += "Shift " + (i+1).ToString() + "\r\n" + decoded_text + "\r\n";
    }

    // ...
    File.WriteAllText(savePath + filename + ".txt", fileContent);
}

答案 1 :(得分:0)

问题是您在decoded_text循环中覆盖了for的内容:

for (int i = 0; i < alphabet.Length; i++)        //Start a loop which will display 25 different candidates of decipher
{
    decoded_text = "";
    foreach (char c in encrypted_text)
    {
        character = c;

        if (character == '\'' || character == ' ')
            continue;

        shift = Array.IndexOf(alphabet, character) - i;     //Define a shift which is the index of a character in an alphabet array, take away the itteration of this loop. Store the result in a variable
        if (shift <= 0)
            shift = shift + 26;

        if (shift >= 26)
            shift = shift - 26;


        decoded_text += alphabet[shift];
    }
    Console.WriteLine("\nShift {0} \n {1}", i + 1, decoded_text);
}

for循环中为其指定一个空字符串。然后在嵌套的foreach循环中为其添加字符。但随后foreach结束,for循环再次迭代并用空字符串擦除它。

因此要么完全删除它,要么将其移动到其他位置,或者在将内容保存到文件之前将其保存到文件中 - 我并非真正理解您正在尝试做的事情。我的意思是,我理解一个凯撒密码,但你的帖子措辞不是很好。

这是设置一些断点的好时机,逐行逐步完成循环并学习使用调试工具。

答案 2 :(得分:-1)

在for循环之前移动行decoded_text=""。这使得文件只包含代码的最后一位。

您可能还想使用StringBuilder而不是附加字符串。这样效率更高。