【问题标题】:StringReader not "exiting"/breakingStringReader 没有“退出”/中断
【发布时间】:2019-05-16 01:56:39
【问题描述】:

我在使用以下代码时遇到问题……当我运行它时,代码永远不会退出。我已经尝试调试了大约一个小时,但我完全不知道问题出在哪里。输出调试();本质上就像 Console.WriteLine();

//Iterate through all lines to find sections
            using (StringReader lineReader = new StringReader(Program))
            {
                int i = 0;
                string line = string.Empty;
                while (line != null)
                {
                    line = lineReader.ReadLine();
                    if (line != string.Empty)
                    {
                        //If the program is exiting (doExit == true), then break the lööp bröther
                    if (doExit)
                            break;

                        //Iterate through all characters in the line
                        foreach (char Character in line)
                    {
                        i++;
                        OutputDebug(i.ToString());
                        if (isOnSameLineAsSectionStart)
                        {
                            sectionName += Character;
                        }
                        else if (Character == ':' && sectionName == string.Empty)
                        {
                            isOnSameLineAsSectionStart = true;
                        }
                        else if (Character == ':' && !isOnSameLineAsSectionStart && sectionName != string.Empty)
                        {
                            OutputDebug("End of section \"" + sectionName + "\" found");
                            OutputDebug(linesInSection.Count() + " lines found total in " + sectionName + "\" during section search");
                            try
                            {
                                sections.Add(sectionName, linesInSection);
                            }
                            catch (Exception)
                            {
                                OutputError("Two/Multiple sections with the same names exist. Ignoring the latest section with the same name");
                            }
                            linesInSection = new List<string>();
                            sectionName = string.Empty;
                            isOnSameLineAsSectionStart = true;
                        }
                    }
                    if (!isOnSameLineAsSectionStart && sectionName != string.Empty)
                    {
                        linesInSection.Add(line);
                    }
                    if (isOnSameLineAsSectionStart && sectionName != string.Empty)
                    {
                        OutputDebug("Start of section \"" + sectionName + "\" found");
                    }
                    if (isOnSameLineAsSectionStart == true)
                    {
                        isOnSameLineAsSectionStart = false;
                    }
                }
                lineReader.Close();
                OutputDebug("In StringReader!" + i);
            }

提前致谢!

【问题讨论】:

  • 是的,看看line 变量的内容是什么(您用作while 循环的退出条件的变量)。你注意到什么了吗?
  • i 永远不会设置为null,因此while 循环的条件永远不会是true(并且不会终止)。
  • 为什么不用string.IsNullOrEmpty()方法?
  • 调试器单步调试时看到了什么?
  • 好的,所以这段代码是你认为有问题的总结,但这段代码甚至不会运行。您必须向我们展示实际存在您描述的问题的代码。我们不是魔术师

标签: c# stringreader


【解决方案1】:

你可以使用下面的方法:

while ((line = reader.ReadLine()) != null)
{
    foreach (char Character in line)
    {
        i++;
        OutputDebug(i.ToString());
    }
}

【讨论】:

  • 谢谢,我试试看!
  • 在 While() 循环中,添加 if (line == null) { break; }
【解决方案2】:

好吧,如果你想逐行输出所有字符。您可以将它们拆分为字符串数组:

var lines = Regex.Split(input, "\r\n|\r|\n") 提取自here

稍后,使用 foreach 而不是 while 语句应该可以解决问题:

foreach(string line in lines)

还将字符串与空值进行比较...看起来不太好。为什么不使用(内置)string.IsNullOrEmpty(line) 方法来检查当前行是否为空?

如果你想使用你的方法,你应该这样做:

while (!string.IsNullOrEmpty(line = reader.ReadLine()))
{
    // Your code...
}

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    由于我们仅限于这个 sn-p,我们只能做出假设。

    lineReader.ReadLine();

    如果这是一个阻塞调用,那么只要没有输入,代码就永远不会退出。

    如果这是一个非阻塞调用,这意味着即使没有提供输入,它也会返回一些东西。如果在这种情况下返回值是空字符串,那么你就处于无限循环中。

    我相信这里的关键功能是ReadLine

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 2018-05-25
      • 2018-05-16
      相关资源
      最近更新 更多