-1

I've posted a question before , here: C# Array of List Index out of bounds Its about the same code. My code is working okay but when it finishes a file , instead of breaking out to the next filename (in a foreach loop) it remains in the same - that way matches always repeats and he just adds the same stuff in different list positions. I've tried changing the position of fs.Close() , adding a break statement but none of those worked.

What am i doing wrong in that case ? On my logical view it should go to the outer loop after it finishes the specific file.

I edited the code to the minimal need - anyway here's a link with the full code: http://pastebin.com/xcKczQLC

This link with the whole code contains the part where i open the file.

Thanks.

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)
Community
  • 1
  • 1
Pablo Costa
  • 125
  • 4
  • 14
  • 1
    Where do you open a file? – Valentin Feb 22 '16 at 14:47
  • Its another part of the code - in order to comply with the community standards i reduced the code to the minimal needed for analysis. But i posted a link to the entire code. – Pablo Costa Feb 22 '16 at 14:51
  • Possible duplicate of [Breaking out of a nested loop](http://stackoverflow.com/questions/324831/breaking-out-of-a-nested-loop) – Keith Hall Feb 22 '16 at 14:52
  • @KeithHall No, not a duplicate – Viru Feb 22 '16 at 14:53
  • I've saw this post too. But is there any solution rather than using a goto or break statement ? I believe there's something involved perhaps with reducing loops (using only one then using a regular for instead of another foreach - maybe) ? – Pablo Costa Feb 22 '16 at 14:54
  • @PabloCosta I don't have access to code by your link. I suggest to move all the logic for one file in one method, and then call this method in foreach. – Valentin Feb 22 '16 at 14:57
  • @PabloCosta when you debug what do you see? I see you have one more loop btween reading each file loop and matches loop – Viru Feb 22 '16 at 15:00
  • Add in the begging of loop fs = new FileStream(fileName, FileMode.Open, FileAccess.Read) – Valentin Feb 22 '16 at 15:03
  • @Valentin in the first loop (where it starts reading the file) the filestream is already like that. var fs = new FileStream(entirefilename, FileMode.Open); – Pablo Costa Feb 22 '16 at 15:08
  • @Sakura thanks for the 'using' . Should've already used. But the problem persists. In the end he returns to the part i pointed in the question - var matches = Regex.Matches(query, pattern); - instead of going back to the outer loop (string filename in fullFileName) – Pablo Costa Feb 22 '16 at 15:16

2 Answers2

1

You can't see it here but in your pastebin, you have a field called filename and a local variable called fileName and you use them interchangeably. You really need to have a good tidy up, consider how you name variables, and break this stuff down into small problems to be able to debug it.

Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
  • So you suggest rebuild the code breaking into functions and then analyze ? I plan to modularize the code later when i get it working. First i'm trying to get things working. – Pablo Costa Feb 22 '16 at 15:06
  • Modularize as you go along. Not doing so is why you're in this state! But the actual problem is with the local variable called `fileName` – Tim Rogers Feb 22 '16 at 15:08
  • I changed its name and changed where it appears in the code - same thing. Also did what @Sakura suggested and added 'using' so it would auto close as it finishes. – Pablo Costa Feb 22 '16 at 15:15
  • Then paste the actual code you are having a problem with. In your pastebin code, you *never use* your `fileName` loop variable. This is clearly a problem. – Tim Rogers Feb 22 '16 at 15:17
  • foreach (string fileName in fullFileName) //Start processing each file - isn't this the fileName loop ? – Pablo Costa Feb 22 '16 at 15:29
  • Correct. Then the next line is `var fs = new FileStream(filename, FileMode.Open);` You have declared `fileName` but you have never actually used it. – Tim Rogers Feb 22 '16 at 15:31
  • I changed its name to entirefilename in the code , and changed in the for statement too. Nothing happened still the same problem. Also i added both vars to local watch and saw their content is okay - fullFileName has 4 entries (based on 4 files i was trying to open) and entirefilename had the current file name. Interesting that he closes this foreach (m in matches), do not close the foreach(string fileName in fullFileName) but won't go to its top (restarting to a new file). – Pablo Costa Feb 22 '16 at 17:51
0

Maybe here is your problem:
Change:

        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

To

        //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

Edit:
use using let you coding safer, it auto close your file when you out of loop:

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