Sean Probyn
Sean Probyn

Reputation: 123

How come it doesn't read the next line in the file?

I'm trying to set the string to read a line out of a txt file which gives the location of the file to do its work with but for some reason it doesn't want to read the next line and keeps producing the same result which is the equivalent to line 0.

private void GetRaceNameButton_Click(object sender, EventArgs e)
    {
        string TestRaceNameFile = "g:\\Racing App\\RaceTestFile.s";
        string TestRaceNameFileUnknown = "g:\\Racing App\\RaceTestFileUnknown.s";

        System.IO.File.WriteAllText(TestRaceNameFile, string.Empty);
        System.IO.File.WriteAllText(TestRaceNameFileUnknown, string.Empty);
        var LinePosition2 = 0;
        String UpdateFileName = File.ReadLines(@"G:\\Racing App\\Update\\all.info").ElementAtOrDefault(LinePosition2);

        StreamReader sr;
        sr = new StreamReader(UpdateFileName);
        var set = sr.ReadToEnd();

        using (StreamReader su = File.OpenText(UpdateFileName))
        {
            string f = String.Empty;
            while ((f = su.ReadLine()) != null)
            {
                var GetRace = new Regex(@"(?<=headline: "")(.*?)(?=""[,])").Matches(set);
                foreach (var Race in GetRace)
                {
                    string RaceToString = Race.ToString();

                    //Sweepstakes                        if (RaceToString.Contains("Sweepstakes") && !RaceToString.Contains("Juvenile") && !RaceToString.Contains("Mares") && !RaceToString.Contains("Apprentice") && !RaceToString.Contains("Amateur") && !RaceToString.Contains("Maiden") && !RaceToString.Contains("Fillies") && !RaceToString.Contains("INH") && !RaceToString.Contains("Beginners") && !RaceToString.Contains("Classified") && !RaceToString.Contains("Conditional") && !RaceToString.Contains("Conditioning") && !RaceToString.Contains("Conditions") && !RaceToString.Contains("National Hunt") && !RaceToString.Contains("Novices") && !RaceToString.Contains("Selling") && !RaceToString.Contains("Handicap") && !RaceToString.Contains("Intermediate") && !RaceToString.Contains("Hunter") && !RaceToString.Contains("Claiming") && !RaceToString.Contains("Hurdle") && !RaceToString.Contains(" Stakes") && !RaceToString.Contains("Chase") && !RaceToString.Contains("Flat"))
                    {
                        File.AppendAllText(TestRaceNameFile, RaceToString + Environment.NewLine);
                        File.AppendAllText(TestRaceNameFile, "Sweepstakes" + Environment.NewLine);
                        File.AppendAllText(TestRaceNameFile, Environment.NewLine);
                    }

                }

            LinePosition2++;
            Console.WriteLine(LinePosition2);

The //Sweepstakes tag is one of 222 as there are this many combinations so I have only shown 1. As far as I know I think that's right but doesn't work. The text file it searches through is like this

g:\analyzer\ht4\1996\August\01-15\01-08-1996\goodwood\result\130988\equity-financial-collections-nursery-stakes-handicap.ht4

210000 lines+ and it just adds the same 2 lines of text to the output file over and over and over again.

Is it something simple I am doing wrong or am I going about this all wrong?

Upvotes: 0

Views: 118

Answers (2)

Oliver
Oliver

Reputation: 45101

Within the loop you apply the RegEx on set all the time, but that variable never changes.

In contrast f holds the content of the current line, but that variable will never be used within your loop.

Upvotes: 1

Peter B
Peter B

Reputation: 24147

You should probably apply the Regex object GetRace to the variable f, like this:

var matchesFound = GetRace.Matches(f);

and then base your if-statement on the contents of matchesFound.
See the MSDN reference.

Also it is expensive and unneeded to create the GetRace Regex object over and over again. In .NET, Regex objects can safely be used multiple times. So it is best to place the line var GetRace = new Regex(....); before the while-loop.

Upvotes: 1

Related Questions