I have the following struct:
public struct StudentDetails
{
public string unitCode; //eg CSC10208
public string unitNumber; //unique identifier
public string firstName; //first name
public string lastName;// last or family name
public int studentMark; //student mark
}
Using that struct I wrote data into a sequential file. The data in the file is as follow:
ABC123
1
John
Doe
95
DCE433
3
Sherlock
Holmes
100
ASD768
5
Chuck
Norris
101
etc.
What’s the best way to read the data from that file and load it into an array of structs?
Assuming your file is one value per line:
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 yourstruct, the following might help to resolve your issue with printing the values:In StudentDetails struct (taken from Nick Bradley’s answer):
Then you could simply loop through the array: