Jackmagic1
Jackmagic1

Reputation: 83

Streamreader giving me what seems random data nd no idea why

I am building a program to read in football leagues and fixtures which are both in the same text file, the legue information is the top 32 lines and the fixtures is the rest of the document. Anyway when I run the program and try to get only the league information (name, sponsor etc) to apper in a listbox I get random lines of code from the whole text. Any idea how I can fix it so only the top 32 lines appear?

 public partial class frmLeaguesAndFixtures : Form
 {
    public static frmLeagues frmkeepLeagues = null;
    ArrayList leaguesArray = new ArrayList();
    public static string inputDataFile = @"E:\Soft130\CW\SOFT130GroupAssignment\SOFT130GroupAssignment\Assignment\bin\Debug\football.txt";
    const int numLeagueItems = 8;

    public frmLeaguesAndFixtures()
    {
        InitializeComponent();
    }

    private bool fileOpenForReadOK(string readFile, ref StreamReader dataIn)
    {
        try
        {
            dataIn = new StreamReader(readFile);
            return true;
        }
        catch (FileNotFoundException notFound)
        {
            MessageBox.Show("ERROR Opening file (when reading data in) - File could not be found.\n"
                                                            + notFound.Message);
            return false;
        }
        catch (Exception e)
        {
            MessageBox.Show("ERROR Opening File (when reading data in)- Operation failed.\n"
                                                            + e.Message);
            return false;
        }
    }

     public static bool getNextLeague(int numItems, StreamReader inNext, string[] nextLeagueData)
     {
        //locals
        string nextLine;
        int numDataItems = numItems;

        //read nextdata - based on constant numDataItems
        for (int i = 0; i < numDataItems; i++)
        {
            try
            {
                nextLine = inNext.ReadLine();
                if (nextLine != null)
                    nextLeagueData[i] = nextLine;
                else
                {
                    return false; //no more full data to process 
                }
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("ERROR Reading from file. Incomplete Data.\n" + e.Message);
                return false; //problem - could not read file
            }
        }
        return true;//no problems 
    }

    private void readLeague()
    {
       string inLeagueName, string inLeagueSponsor, int inLeaguePrize, int inLeagueNumFixtures)

        //local variables
        StreamReader inLeague = null;
        Leagues tempLeague;
        bool anyMoreLeagues = false;
        string[] leagueData = new string[numLeagueItems];

        //if file opened ok proceed
        if (fileOpenForReadOK(inputDataFile, ref inLeague))
        {
         //read first league     
         anyMoreLeagues = getNextLeague(numLeagueItems,inLeague, leagueData);

            //loop for all FULL league   in file
            while (anyMoreLeagues == true)
            {
                //create new league
                tempLeague = new Leagues(leagueData[0], leagueData[1], leagueData[2], leagueData[3]);

                //store in array
                leaguesArray.Add(tempLeague);

                //DOESNT WORK CORRECTLY TRY IT USING "PREMIERSHIP"
                // if (leagueData[0] == "The Championship")
                    {
                        foreach (Leagues l in leaguesArray)
                        {
                            lstLeagueInfo.Items.Add(l.getLeagueName() + " " + l.getLeagueSponsor() + " " + l.getLeaguePrize() + " " + l.getLeagueNumFixtures());
                        }
                    }

                    //read next data
                    anyMoreLeagues = getNextLeague(numLeagueItems,inLeague, leagueData);

                } //end while - no more 
            }//end if file ok            
        }


    private void btnPremiership(object sender, EventArgs e)
    {
        readLeague();
    }
}

Upvotes: 0

Views: 119

Answers (2)

Dilshod
Dilshod

Reputation: 3331

and use this to get the rest of the lines.

var allLines = File.ReadeLines(filename).ToList();
var first32 = allLines.Take(32).ToList();
var rest = allLines.Skip(32).ToList();

I hope this helps

UPDATE 1:

To go through those lines use foreach.

//example
foreach(string line in first32)
{
    //do your work
}

Upvotes: 0

I4V
I4V

Reputation: 35363

var top32Lines = File.ReadLines(filename).Take(32).ToList();

Upvotes: 3

Related Questions