2013-04-12 72 views
0

我建立一個程序在足球聯賽和夾具這兩者都是在同一個文本文件中讀取數據,legue信息是頂級的32線和燈具是休息該文件。無論如何,當我運行程序並嘗試獲取聯盟信息(名稱,贊助商等)在列表框中顯示時,我會從整個文本中獲取隨機代碼行。任何想法如何解決它,所以只有最高32行出現?的StreamReader給我什麼,似乎隨機數據n不知道爲什麼

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(); 
    } 
} 

回答

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

您好我試過,但我剛開(集合)作爲輸出,我該如何解決這個問題? – Jackmagic1

+0

@ user2190867'string singleString = String.Join(Environment.NewLine,top32Lines);'那樣的東西? – I4V

0

,並用它來獲得行的其餘部分。

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

我希望這有助於

更新1:

要經過的線路使用foreach

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

嗨我試過這個,但我只是得到(集合)作爲輸出,我該如何解決這個問題? – Jackmagic1

+0

@ user2190867它返回列表。你需要什麼樣的輸出? – Dilshod

相關問題