Assuming your file is one value per line:
List<StudentDetails> studentList = new List<StudentDetails>();
using (StreamReader sr = new StreamReader(@"filename"))
{
while (!sr.EndOfStream)
{
StudentDetails student;
student.unitCode = sr.ReadLine();
student.unitNumber = sr.ReadLine();
student.firstName = sr.ReadLine();
student.lastName = sr.ReadLine();
student.studentMark = Convert.ToInt32(sr.ReadLine());
studentList.Add(student);
}
StudentDetail[] studentArray = studentList.ToArray();
}
Note that this is not very robust - if there are not 5 lines for each student, you'll run into problems, or if the last student has less than 5 lines.
EDIT
Taking the lessons learned from your previous question Array of structs in C# regarding the need to override ToString()
in your struct
, the following might help to resolve your issue with printing the values:
In StudentDetails struct (taken from Nick Bradley's answer):
public override string ToString()
{
return string.Format("{0}, {1}, {2}, {3}, {4}", unitCode,
unitNumber, firstName, lastName, studentMark);
}
Then you could simply loop through the array:
for (int i = 0; i < studentArray.Length; i++)
{
Console.WriteLine("Student #{0}:", i);
Console.WriteLine(studentArray[i]);
Console.WriteLine();
}