从顺序文件读入结构数组

发布于 2024-11-29 14:40:24 字数 516 浏览 3 评论 0原文

我有以下结构:

    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
    }

使用该结构我将数据写入顺序文件。文件中的数据如下:

ABC123
1
John
Doe
95
DCE433
3
Sherlock
Holmes
100
ASD768
5
Chuck
Norris
101

等。

从该文件读取数据并将其加载到结构数组中的最佳方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗藏城府 2024-12-06 14:40:24

假设您的文件每行一个值:

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

}

请注意,这不是很稳健 - 如果每个学生没有 5 行,或者如果最后一个学生的行数少于 5 行,您就会遇到问题。

编辑

从上一个问题中吸取教训C# 中的结构数组< /a> 关于需要重写 struct 中的 ToString(),以下内容可能有助于解决打印值的问题:

在 StudentDetails 结构中(取自 Nick Bradley 的回答):

public override string ToString()
{
    return string.Format("{0}, {1}, {2}, {3}, {4}", unitCode,
           unitNumber, firstName, lastName, studentMark);
}

然后你可以简单地循环数组:

for (int i = 0; i < studentArray.Length; i++)
{
    Console.WriteLine("Student #{0}:", i);
    Console.WriteLine(studentArray[i]);
    Console.WriteLine();
}

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();
}
不顾 2024-12-06 14:40:24

最初,我使用某种 序列化 来写入文件,因为它还会处理阅读部分。
但考虑到您创建文件的方式,我使用 StreamReader< /a> 它是 ReadLine() 方法 - 因为您很快就知道写入服务器的属性的顺序:

string line = "";
while ((line = reader.ReadLine()) != null)
{
  YourStruct t = new YourStruct();
  t.unitCode = line;
  t.unitNumber = reader.ReadLine();
  ...
  resultArray.Add(t);
}
reader.Close(); reader.Dispose();

Initially I'de use some sort of Serialization to write to the file since it will also take care of the reading part as well.
But given the way you created the file, I'de use StreamReader and it's ReadLine() method - since you know what the order of the properties you wrote to the server a simple while:

string line = "";
while ((line = reader.ReadLine()) != null)
{
  YourStruct t = new YourStruct();
  t.unitCode = line;
  t.unitNumber = reader.ReadLine();
  ...
  resultArray.Add(t);
}
reader.Close(); reader.Dispose();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文