[Solved] Auto-generate a list of words in C#, and save as File [closed]


The solution I came up with is as follows:

public void AppendFile(string filePath, long firstWord, long lastWord)
{            
    using (StreamWriter sw = File.AppendText(filePath))
    {
        for (long i = firstWord; i < lastWord; i++)
        {
            sw.WriteLine(GetWord(i));

        }

    }

}

public void AppendFile(string filePath, long lastWord)
{
    AppendFile(filePath, 0, lastWord);

}

public void AppendFile(string filePath)
{
    AppendFile(filePath, long.MaxValue);

}

public static string GetWord(long i)
{
    string s = Encoding.ASCII.GetString(new byte[] { (byte)(i % 128) });

    if (i < 128)
        return s;

    return GetWord(i / 128) + s;

}

Use in the following way:

AppendFile("words.txt"); // Will most likely fall over or at least take a long time

AppendFile("words.txt", 1000); // This is the method requested

AppendFile("words.txt", 500, 1000); // Extended functionality

Note: I chose to NOT use the algorithm from Steve’s answer. The reason I did not use Steve’s algorithm is because he relies on the full array of words being resident in memory during the full procedure, which restricts the output file to the amount of free available RAM. My version doesn’t have this restriction, and is restricted only to the max possible length of a string.

solved Auto-generate a list of words in C#, and save as File [closed]