Hello,

I have a Text file containing the english dictionary, it is 237,000 words long and in each line of the text file there is a word.

What I want to do is obtain a word from the textfile randomly. I don't think reading the text file to end and storing it in an array is an efficient solution. Is there a way I could do to skip reading all the file and just access a random line?

Oh, another thing how can I add the text file to the project so I can use a relative link to the file, since I have to use a complete path in my harddrive and I don't think that will work when I put the project in another computer.

Thanks

Edited 5 Years Ago by MrCapuchino: n/a

public static string GetRandomLine(ref string file)
{
    //Generic list for holding the lines
    List<string> lines = new List<string>();

    //Random class to generate our random number
    Random rnd = new Random();

    //Variable to hold our random line number
    int i = 0;

    try
    {
        if (File.Exists(file))
        {
            //StreamReader to read our file
            StreamReader reader = new StreamReader(file);
            
            //Now we loop through each line of our text file
            //adding each line to our list
            while (!(reader.Peek() == -1))
                lines.Add(reader.ReadLine());

            //Now we need a random number
            i = rnd.Next(lines.Count);

            //Close our StreamReader
            reader.Close();

            //Dispose of the instance
            reader.Dispose();

            //Now write out the random line to the TextBox
            return lines[i].Trim();
        }
        else
        {
            //file doesn't exist so return nothing
            return string.Empty;
        }
    }
    catch (IOException ex)
    {
        MessageBox.Show("Error: " + ex.Message);
        return string.Empty;
    }

}

for file path
you put the file in the \debug folder
and use

string filepath = System.IO.Directory.GetCurrentDirectory() + "\\filename";

Edited 5 Years Ago by darkseid: n/a

1. Generate a random number with Random class. There must be an option in StreamReader/TextReader to get block from the specific location.

2. If that text file is in the same folder where application is, use just file's name eg.

StreamReader reader = new StreamReader("file.txt");

Hi,

Thanks for your answer

darkseid, I think that your solution would not work very efficiently with me since the text file is very large and I don't want to waste resources, what I want is not having to store all the text file in memory.

jugosoft, what do you mean with block of specific location can you help me throught?

Thanks

This is the best possible solution I will give you now that I know.
YOu cannot read only one line from a text file, like read at index. No, you can read some random line, but until that line, the code will read all the lines from 1st to that line you want.

string path = @"C:\1\test5.txt";
            int allRows = File.ReadAllLines(path).Length;
            Random random = new Random();
            int myLine = random.Next(0, allRows);
            using (Stream stream = File.Open(path, FileMode.Open))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    string line = null;
                    for (int i = 0; i < myLine; ++i)
                    {
                        line = reader.ReadLine();
                    }
                    Console.WriteLine("Randonly selected text: {0}", line);
                }
                Console.ReadLine();
            }

I understand, it is a nice solution, What I will do is shorten the dictionary erasing the word that I don't need then use your coude.

Thank You

This article has been dead for over six months. Start a new discussion instead.