我正在构建一个程序,用于阅读同一文本文件中的足球联赛和灯具,其中legue信息是前32行,灯具是文档的其余部分。无论如何,当我运行程序并尝试仅在列表框中获取联盟信息(名称,赞助商等)时,我会从整个文本中获得随机代码行。知道如何解决这个问题只有前32行出现了吗?
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();
}
}
答案 0 :(得分:3)
var top32Lines = File.ReadLines(filename).Take(32).ToList();
答案 1 :(得分: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
}