0

我正在尝试制作一个字典游戏,我有一个文本文件,每个文件都有大约 100,000 个单词。我有这个代码:

   words = new List<Word>();
   Console.WriteLine("Please wait, compiling words list...");
   TextReader tr = new StreamReader(DICT);
   string line = tr.ReadLine();
   while (line != "" && line != null) {
    words.Add(new Word(line));
    line = tr.ReadLine();
   }
   Console.WriteLine("List compiled with " + words.Count + " words.");

但是,它停在 40510 字。为什么是这样?我该如何解决这个问题?

谢谢你。

4

4 回答 4

2

编辑:对不起;我检查了记事本中的空白行,但没有找到;在记事本++中搜索找到了它们。

我的错,还是谢谢你。

于 2010-07-28T17:55:13.990 回答
1

它只是停止还是抛出异常?在 Console.WriteLine 调用之前检查line调试器中的变量值,那里可能是空行。

于 2010-07-28T17:55:09.403 回答
0

问题是你的行!=“”检查。删除它,它将继续。

于 2010-07-28T17:56:02.103 回答
0

问题似乎是你的while{}循环。

我会做这样的事情:

words = new List<Word>(); 
Console.WriteLine("Please wait, compiling words list..."); 
TextReader tr = new StreamReader(DICT); 
string line;
while((line = tr.ReadLine()) != null)
if(!string.IsNullOrEmpty(line.Trim()))
{ 
 words.Add(new Word(line)); 
} 
Console.WriteLine("List compiled with " + words.Count + " words.");

I haven't tested that, so there could be some errors, but the big thing is that your while{} loop will break on the first blank line instead of just discarding it. In this example, that is corrected, and it will only break when there are no more lines to read.

于 2010-07-28T18:25:11.690 回答