简体   繁体   中英

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. I have an issue with writing it because it only writes one 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. 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);
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM