Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6319665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:50:41+00:00 2026-05-24T15:50:41+00:00

I have the following struct: public struct StudentDetails { public string unitCode; //eg CSC10208

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:50:42+00:00Added an answer on May 24, 2026 at 3:50 pm

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following struct: public struct Declarations { public const string SchemaVersion =
I have the following code: #include <map> #include <string> class policy1 { public: struct
I have some generic types, like the following: public struct Tuple<T1, T2> { ...
I have the following struct in C# class public struct Employee { public const
I'm writing some Enum functionality, and have the following: public static T ConvertStringToEnumValue<T>(string valueToConvert,
I have the following structure: private struct S_indiv { public int[] x; public int
Say I have a struct declared like the following: public struct Test { public
I have the following struct defined in a user control: public struct ColumnData {
I have the following structure: [StructLayout(LayoutKind.Sequential)] public struct TCurve { public int fNumItems; /*
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.