[英]Outputting the content of an array to a text file in C#
I need your help.我需要你的帮助。 I want to make a program where I give a number and it increments it as many times as I said and write output into a.txt file.
我想制作一个程序,在其中我给出一个数字,并按照我所说的多次递增它并将 output 写入 .txt 文件。 I have an issue with writing it because it only writes one output.
我写它有问题,因为它只写一个 output。 I know basics but I have never worked with more complicated coding, only a few games, and simple apps.
我知道基础知识,但我从未使用过更复杂的编码,只有少数游戏和简单的应用程序。
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++;
}
you can use this code.您可以使用此代码。 You wrote the number of for loops in the file, which is wrong.
您在文件中写入了 for 循环的数量,这是错误的。 Put the text in a string and put it in the file
将文本放入字符串并放入文件中
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);
Read text file for editing读取文本文件进行编辑
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.