从文件读取时出现无限循环问题

时间:2015-04-10 17:49:37

标签: c# list streamreader

我正在用C#编写一个程序来从一个文件中读取并输出到csv文件中的每个单词的所有唯一字和文件中出现的次数。我的问题是,当我尝试运行我的程序时,我永远不会退出逐行的while循环。

public override List<WordEntry> GetWordCount()
{
        List<WordEntry> words = new List<WordEntry>();
        WordEntry wordEntry = new WordEntry();
        //string[] tokens = null;
        string line, temp, getword;
        int count = 0, index = 0;
        long number;

        while ((line = input.ReadLine()) != null)
        {
            if (line == null)
                Debug.Write("shouldnt happen");
            char[] delimit = { ' ', ',' };
            string[] tokens = line.Split(delimit);

            if (words.Count == 0)
            {
                wordEntry.Word = tokens[0];
                wordEntry.WordCount = 1;
                words.Add(wordEntry);
            }//end if

            for (int i = 0; i < tokens.Length; i++)
            {
                for (int j = 0; j < words.Count; j++)
                {
                    if (tokens[i] == words[j].Word)
                    {
                        number = words[j].WordCount;
                        number++;
                        getword = words[j].Word;
                        wordEntry.WordCount = number;
                        wordEntry.Word = getword;
                        words.RemoveAt(j);
                        words.Insert(j, wordEntry);
                    }//end if
                    else
                    {
                        wordEntry.Word = tokens[i];
                        wordEntry.WordCount = 1;
                        words.Add(wordEntry);
                    }//end else
                }//end for
            }//end for
        }//end while
        return words;
}

它在while循环中陷入困境,好像它永远不会到达文件的末尾。该文件是2.6 MB,所以它应该能够到最后。

2 个答案:

答案 0 :(得分:3)

以下是如何重写代码以使用字典。

var words = new Dictionary<string,int>();

while ((line = input.ReadLine()) != null)
{
    if (line == null)
        Debug.Write("shouldnt happen");
    char[] delimit = { ' ', ',' };
    string[] tokens = line.Split(delimit);

    foreach (var word in tokens)
    {
        if(words.ContainsKey(word))
            words[word]++;
        else
            words.Add(word, 1);
    }
}

这降低了代码的复杂性,因为字典具有O(1)查找。

修改

您可以将字典转换为List<WordEntry>,就像这样。

return words
    .Select(kvp => new WorkEntry
        {
            Word = kvp.Key, 
            WordCount = kvp.Value
        })
    .ToList();

答案 1 :(得分:1)

我想事实上你的代码并没有离开&#34; for(int j = 0; j&lt; words.Count; j ++)&#34;因为新项目会被添加到单词列表中。