1

So I'm trying to read each line of a text file with streamreader, from there I go into a while loop to get to the end of the file the for loop is to print each of the tokens to the listbox. I feel like this should work!

EDIT: My question is how do I read a selected file, separate the words, and print them to the listbox?

  if (openFile.ShowDialog() == DialogResult.OK)
            {
                StreamReader inputFile;
                inputFile = File.OpenText(openFile.FileName);
                string line;
                //int totalWords;

                char[] delim = { '.', '!', '?', ',', '(', ')' };

                while (!inputFile.EndOfStream)
                {
                    line = inputFile.ReadLine();
                    string[] tokens = line.Split(delim);
                    for (int i = 0; i < tokens.Length; i++)
                    {
                        wordListBox.Items.Add(tokens[i]);
                    }
                }
              inputFile.Close();
            }
Fitz
  • 23
  • 6
  • 6
    So, what's your question? Do you want to know if we have the same feeling? – CharithJ Jun 14 '17 at 00:52
  • I would use StreamReader in a using block. Eg: using (StreamReader inputFile = File.OpenText(openFile.FileName)) – CharithJ Jun 14 '17 at 00:53
  • 1
    From the [help/on-topic]: *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.* I see no *specific problem or error* (I don't even see a vague one). I don't see any sort of a question, either. You may want to take a few minutes to read through [ask] and then come back and [edit] your post to make it more clear. – Ken White Jun 14 '17 at 00:55
  • First of all you question is bad. So if you asked in the right way you would see its a [duplicate](https://stackoverflow.com/questions/16725848/how-to-split-text-into-words) – Filip Cordas Jun 14 '17 at 08:38
  • Possible duplicate of [How to split text into words?](https://stackoverflow.com/questions/16725848/how-to-split-text-into-words) – Filip Cordas Jun 14 '17 at 08:38

3 Answers3

2

What if you add the whitespace characters, '\n', '\r', '\t', and ' ' to your delimeter array? Then you can just call File.ReadAllText, which returns the whole file as a string, and split it on your delimeters (while removing empty entries).

After that, you have an array of words that you can add to your ListBox:

if (openFile.ShowDialog() == DialogResult.OK)
{
    char[] delims = { '.', '!', '?', ',', '(', ')', '\t', '\n', '\r', ' ' };

    string[] words = File.ReadAllText(openFile.FileName)
        .Split(delims, StringSplitOptions.RemoveEmptyEntries);

    foreach (string word in words)
    {
        wordListBox.Items.Add(word);
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

If you trying to fill ListBox with words from file using StreamReader - you should think about it, because StreamReader is used in File/Network streams cases, to handle large files or network latency/propagation and so on. And if you have a large file - is it a good practice to fill ListBox too many items? I dont think so. But, according to your question, if you want to do it using StreamReader, check this implementation:

        string filename = @"D:\text.txt";
        var words = new List<string>();
        char[] delims = { '.', '!', '?', ',', '(', ')', '\t', '\n', '\r', ' ' };

        try
        {
            using (var reader = new StreamReader(filename))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    words.AddRange(line.Split(delims, StringSplitOptions.RemoveEmptyEntries));
                }
            }
        }

        // now you dont need to close stream because
        // using statement will handle it for you

        catch // appropriate exception handling
        {

        }

        foreach (string word in words)
        {
            wordListBox.Items.Add(word);
        }
Nikita V
  • 367
  • 4
  • 9
-1

This Does work.... mostly. the txt document was still running the in the background. However the new lines (\n) and (\t) are not seperated with the delimiter. for this I believe the split function needs to be used.

Thanks for the help.

Fitz
  • 23
  • 6