【问题标题】:Lost in a nested loop迷失在嵌套循环中
【发布时间】:2016-02-22 23:06:57
【问题描述】:

我之前发过一个问题,这里: C# Array of List Index out of bounds 它的代码大致相同。我的代码工作正常,但是当它完成一个文件时,它没有中断到下一个文件名(在 foreach 循环中),它保持不变 - 这样匹配总是重复,他只是在不同的列表位置添加相同的东西。我试过改变 fs.Close() 的位置,添加一个 break 语句,但这些都不起作用。

在这种情况下我做错了什么?在我的逻辑视图中,它应该在完成特定文件后转到外循环。

我根据最低需求编辑了代码 - 无论如何,这里有一个完整代码的链接: http://pastebin.com/xcKczQLC

整个代码的这个链接包含我打开文件的部分。

谢谢。

foreach (string fileName in fullFileName) //Start processing each file
{
    // Lots of code here - reading a mixed file - no need to show

        bool b = filecontents.Contains("CIP3EndOfFile"); 
        if(b)
        {
            //Code manipulating contents in filecontents (a string)




            var matches = Regex.Matches(query, pattern);
            //When the foreach (Match m in matches) loop ends he always returns to the line above
            finalcontent[listpos] = new List<xmldata>();//Initializing a list for each filename

            foreach (Match m in matches)
            {

           //Lots of code doing some operations with the found match 

                if (i < colors_str.Length)
                {
                    finalcontent[listpos].Add(new xmldata//The exception is thrown right here
                    {
                        colorname = colors_str[i],
                        colorvalues = values,

                    });//Closing list add declaration
                 }//Closing if

                i++;
            }//Closing foreach loop
            if (i >= colors_str.Length)
            {
                listpos++;
                i = 0;
            }
            fs.Close();//closing current file
            //In this moment i expected that it would go for the foreach (string fileName in fullFileName) loop
            //Instead he returns to this inner foreach loop
        }//Closing boolean if

    }//End file reading - closing for




}//Finished processing each filename (string filename in filename)

【问题讨论】:

  • 你在哪里打开文件?
  • 它是代码的另一部分——为了符合社区标准,我将代码减少到分析所需的最低限度。但我发布了整个代码的链接。
  • @KeithHall 不,不是重复的
  • 我也看过这篇文章。但是除了使用 goto 或 break 语句之外,还有什么解决方案吗?我相信可能涉及减少循环(仅使用一个然后使用常规 for 而不是另一个 foreach - 也许)?

标签: c# foreach nested-loops


【解决方案1】:

您在这里看不到它,但在您的 pastebin 中,您有一个名为 filename 的字段和一个名为 fileName 的局部变量,您可以互换使用它们。你真的需要好好整理一下,考虑如何命名变量,并将这些东西分解成小问题以便能够对其进行调试。

【讨论】:

  • 所以你建议重建代码分解成函数然后分析?我计划稍后在我开始工作时对代码进行模块化。首先,我试图让事情正常进行。
  • 随您进行模块化。不这样做就是你处于这种状态的原因!但实际问题在于名为 fileName 的局部变量
  • 我更改了它的名称并更改了它在代码中出现的位置——同样的事情。还做了@Sakura 的建议并添加了“使用”,所以它会在完成时自动关闭。
  • 然后粘贴您遇到问题的实际代码。在您的 pastebin 代码中,您从不使用您的 fileName 循环变量。这显然是个问题。
  • foreach (string fileName in fullFileName) //开始处理每个文件——这不是文件名循环吗?
【解决方案2】:

也许这是你的问题:
变化:

        fs.Close();//closing current file
        //In this moment i expected that it would go for the foreach (string fileName in fullFileName) loop
        //Instead he returns to this inner foreach loop
    }//Closing boolean if

        //In this moment i expected that it would go for the foreach (string fileName in fullFileName) loop
        //Instead he returns to this inner foreach loop
    }//Closing boolean if
fs.Close();//closing current file

编辑:
使用using 让你编码更安全,当你跳出循环时它会自动关闭你的文件:

foreach (string fileName in fullFileName) //Start processing each file
{
    using (var fs =new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
    {
    }
}

【讨论】:

  • 相同的行为。他确实执行了 fs.Close() 但返回到我在问题中提到的同一点。
  • 将 fs 设置为空并在下一个循环中重新初始化。该对象可能会保留以前的值。
  • @PabloCosta 不一样。在您的代码中,您仅在 if(b) 为真时关闭文件,我的代码将在这两种情况下关闭文件。
  • @Tom 你是什么意思设置 fs 为空。我对 C# 有点陌生
  • @PabloCosta fs.Close(); fs = null
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 2023-03-02
  • 2020-10-09
  • 2015-01-28
  • 2013-10-26
  • 1970-01-01
相关资源
最近更新 更多