C#列表还是文本阅读器限制?
我正在尝试制作一个字典游戏,我有一个文本文件,每行大约有100000个单词。我有以下代码:C#列表还是文本阅读器限制?,c#,list,textreader,C#,List,Textreader,我正在尝试制作一个字典游戏,我有一个文本文件,每行大约有100000个单词。我有以下代码: words = new List<Word>(); Console.WriteLine("Please wait, compiling words list..."); TextReader tr = new StreamReader(DICT); string line = tr.ReadLine(); while (line != "" &&
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.");
words=新列表();
Console.WriteLine(“请稍候,正在编译单词列表…”);
TextReader tr=新的StreamReader(DICT);
字符串行=tr.ReadLine();
while(line!=“”&&line!=null){
添加(新词(行));
line=tr.ReadLine();
}
Console.WriteLine(“用“+单词.计数+”单词编译的列表”);
然而,它停在40510字。为什么会这样?我怎样才能免除这个问题呢
谢谢。它只是停止还是抛出异常?在Console.WriteLine调用之前,检查调试器中的
line
变量值,可能是空行 它只是停止还是抛出异常?在Console.WriteLine调用之前,检查调试器中的line
变量值,可能是空行 编辑:对不起;我检查了一下记事本上的空行,没有发现;在记事本++中搜索已找到它们
我的错,谢谢你。编辑:对不起;我检查了一下记事本上的空行,没有发现;在记事本++中搜索已找到它们
我的错,谢谢你。问题在于你的线路!=”“好的。删除该选项,它将继续。问题出在您的线路上!=”“好的。删除它,它将继续。问题似乎是您的
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.");
words=新列表();
Console.WriteLine(“请稍候,正在编译单词列表…”);
TextReader tr=新的StreamReader(DICT);
弦线;
而((line=tr.ReadLine())!=null)
如果(!string.IsNullOrEmpty(line.Trim()))
{
添加(新词(行));
}
Console.WriteLine(“用“+单词.计数+”单词编译的列表”);
我还没有测试过,所以可能会有一些错误,但最重要的是你的
while{}
循环将在第一个空行中断,而不仅仅是丢弃它。在本例中,这一点已得到纠正,只有当没有更多行可读取时才会中断。问题似乎是您的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.");
words=新列表();
Console.WriteLine(“请稍候,正在编译单词列表…”);
TextReader tr=新的StreamReader(DICT);
弦线;
而((line=tr.ReadLine())!=null)
如果(!string.IsNullOrEmpty(line.Trim()))
{
添加(新词(行));
}
Console.WriteLine(“用“+单词.计数+”单词编译的列表”);
我还没有测试过,所以可能会有一些错误,但最重要的是你的
while{}
循环将在第一个空行中断,而不仅仅是丢弃它。在本例中,这一点已更正,只有在没有更多行可读取时才会中断。我已检查是否有空行;但是如何检查空字符呢?编辑:Notepadd++显示更多信息…我已检查是否有空行;但是如何检查空字符呢?编辑:Notepad++揭示了更多信息……是的,不要将记事本用于任何与编程相关的内容。将记事本++切换为默认值。是的,不要将记事本用于任何与编程相关的内容。将Notepad++切换为默认设置。