简体   繁体   中英

Reading and deleting a Random Line from a .txt File in C#

I wanted to make a random number generator in a C# console, which didn't show a number a second time. So I made a script which picks a random Number from a.txt file, reads it and deletes it from the.txt afterwards.

I know that there is no section for reading the line and giving an output, because I first wanted to get the part with deleting. The thing was, it just deleted the full.txt file.

Script:

using System;
using System.IO;

namespace Random_Number_generator
{
    class Program
    {
        static void Main(string[] args)
        {
            //Generates the random Number
            int RandomNumber;
            string BGInfo;
            Random rnd = new Random();
            int GetRandomInt(int min, int max)
            {
                return rnd.Next(min, max);
            }
            RandomNumber = GetRandomInt(1, 25);

            // 1. Read the content of the file
            string[] readText = File.ReadAllLines("D:/BG_Numbers.txt");
            Console.WriteLine("Readed: " + readText);
            Console.ReadKey();

            // 2. Empty the file
            File.WriteAllText("D:/BG_Numbers.txt", String.Empty);

            using (StreamWriter writer = new StreamWriter("D:/BG_Numbers.txt"))
            {
                foreach (string s in readText)
                {
                    if (!s.Equals(RandomNumber))
                    {
                        writer.WriteLine(s);
                    }
                }
            }
        }
    }
}

When you're comparing the number you read from the file with the randomly generated number, you're actually comparing the string with int therefore your condition is never true . You can either change your random number to string by using its ToString value or convert the lines from the file to actual numbers while reading file content as I did in the example below:

const string fileName = "D:/BG_Numbers.txt";

// Generate random number
int randomNumber;
Random random = new Random();
randomNumber = random.Next(1, 25);
Console.WriteLine($"Your random number is: {randomNumber}");
Console.ReadKey();

// Read the file content
int[] numbers = File.ReadAllLines(fileName).Select(int.Parse).ToArray();
Console.WriteLine($"Read: [{string.Join(", ", numbers)}]");
Console.ReadKey();

// Clear the file content
File.WriteAllText(fileName, string.Empty);
Console.WriteLine("File content cleared.");
Console.ReadKey();

using (StreamWriter writer = new StreamWriter(fileName))
{
    foreach (int number in numbers)
    {
        if (!number.Equals(randomNumber)) // This line was comparing 'string' with 'int'
        {
            writer.WriteLine(number);
        }
    }
}

Console.WriteLine("Done!");

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