[英]Outputting the content of an array to a text file in C#
我需要你的幫助。 我想制作一個程序,在其中我給出一個數字,並按照我所說的多次遞增它並將 output 寫入 .txt 文件。 我寫它有問題,因為它只寫一個 output。 我知道基礎知識,但我從未使用過更復雜的編碼,只有少數游戲和簡單的應用程序。
int baseNumber = int.Parse(tbBase.Text);
int codeNumber = int.Parse(tbCodeNumber.Text);
int[] codes = new int[codeNumber];
int count = 0;
string link = "https://www.roblox.com/groups/";
for (int i =0; i < codes.Length; i++)
{
codes[i] = baseNumber++;
}
foreach (int element in codes)
{
string text= $"{link}{Convert.ToString(codes[count])}";
System.IO.File.WriteAllText(@"D:\TextFiles\WriteText.txt", text);
count++;
}
您可以使用此代碼。 您在文件中寫入了 for 循環的數量,這是錯誤的。 將文本放入字符串並放入文件中
int baseNumber = int.Parse(tbBase.Text);
int codeNumber = int.Parse(tbCodeNumber.Text);
int[] codes = new int[codeNumber];
for (int i = 0; i < codes.Length; i++)
{
codes[i] = baseNumber++;
}
int count = 0;
string text = "";
string link = "https://www.roblox.com/groups/";
foreach (int element in codes)
{
text += $"{link}{Convert.ToString(codes[count])}";
count++;
}
string fileName = @"D:\WriteText.txt";
if (File.Exists(fileName))
System.IO.File.AppendAllText(fileName, text);
else
System.IO.File.WriteAllText(fileName, text);
讀取文本文件進行編輯
public void ReadFile(string fileName)
{
string[] lines = System.IO.File.ReadAllLines(fileName);
// Display the file contents by using a foreach loop.
string text = "";
foreach (string line in lines)
{
// Use a tab to indent each line of the file.
text += line + Environment.NewLine;
Console.WriteLine("\t" + line);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.