2015-12-02 83 views
-2

我正在創建一個凱撒密碼解密/加密程序。我的程序從.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 ////////// 
+0

有什麼問題嗎? –

回答

4

問題是這條線string fileContent = decoded_text; 您只是將fileContent分配給最後解碼的文本。要在循環之前修復這個put fileContent,然後在每個步驟中添加每個decodedText。

像這樣:

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); 
} 
+0

謝謝埃米爾。不勝感激! –

+2

並且不要使用字符串連接將文件名添加到目錄名稱中:使用'Path.Combine(savePath,filename +「.txt」)',這樣您就可以處理包含和不包含尾隨分隔符的兩個目錄。 – CodeCaster

+1

好的,感謝@CodeCaster的建議,非常感謝。 :) –

-1

招行decoded_text=""前的for循環。這使得文件只包含代碼的最後一位。

您也可能想使用StringBuilder而不是附加字符串。這樣更有效率。

+0

這看起來像是對我的正確答案。也許你因爲可以更好地解釋投票而陷入低票? – Niklas

+2

這個答案並沒有真正地說什麼 - 這甚至意味着什麼? –

+0

@Niklas怎麼樣? 「put」是什麼意思?它已經被聲明在循環之外。 – CodeCaster

0

的問題是,你在寫作中你for循環decoded_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); 
} 

你指定在你for環空字符串。然後在嵌套的foreach循環中爲其添加字符。但是,然後foreach結束,for循環再次迭代並用空字符串擦除它。

因此,要麼完全刪除它,要麼將其移動到別處,或者在清除之前將內容保存到文件中 - 我完全不明白您要做什麼。我的意思是,我瞭解凱撒密碼,但是你的帖子並不是很好。

這將是一個很好的時間來設置一些斷點,逐行掃描循環並學會使用調試工具。

+1

如果你從你的答案中刪除了OP的不變的代碼和你的「去調試」的提示,你留下了_「問題是,你正在over_write你的for循環中的decode_text的內容[...]刪除完全地,或者將它移動到其他地方,或者在清除「_」之前將內容保存到文件中。你能詳細解釋一下嗎? – CodeCaster

+0

正如我所說的,我並不完全明白他究竟想要做什麼,這就是爲什麼我沒有詳細說明。埃米爾已經明白了;在循環前定義'fileContent'並在清除之前添加'decoded_text'。但是,對,我猜你是對的。這本身並不是真正的*解決方案*,只是說他做錯了什麼:/ – sab669

+0

_「我完全不明白他到底在做什麼」 - 然後發表評論,而不是回答。 :) – CodeCaster

相關問題