Home Articles

使用文件c#中的vector属性读取对象

Asked
Viewed 944 times
1

我有这门课:

public class Student
{
private int[] note;//vector de note
        private DateTime dataInrolarii;
        private int nrRestante; 
        private readonly int nrMatricol; 
        private const string sex = "feminin"
        public static int anStudiu = 3; 

 public Student(string cnp, string nume, int nrMatricol, DateTime dataInrolarii, int nrRestante, int[] note) : base(cnp, nume)
        {
            this.nrMatricol = nrMatricol;
            this.dataInrolarii = Convert.ToDateTime(dataInrolarii);
            this.nrRestante = nrRestante;
            this.note = new int[note.Length];
            for (int i = 0; i < note.Length; i++)
                this.note[i] = note[i];
        }
}

当然,我有getter,setter,toString()重载等等 . 我有一个包含此对象的文本文件:

2920118080023, George, 2, 12/09/2016, 4, 10 3 4

我想从文件中读取并在文本框中显示来自文件的学生 . 但我真的不知道如何用矢量笔记做到这一点 .

这是我写的:

private void button_CitireTXT_Click(object sender, EventArgs e)
    {

        StreamReader sr = new StreamReader("citiretxt.txt");
        string linie = null;
        while ((linie = sr.ReadLine()) != null)
        {
            try
            {//oricare ar fi tipul unui atribut citit din fisier, ele sunt STRING acolo
                string cnp = linie.Trim().Split(',')[0];
                string nume = linie.Trim().Split(',')[1];
                int nrmat = Convert.ToInt32(linie.Trim().Split(',')[2]);
                string datainrolString = linie.Trim().Split(',')[3];
                DateTime datainrol = DateTime.Parse(datainrolString);
                int nrRest = Convert.ToInt32(linie.Trim().Split(',')[4]);

                string noteString = linie.Split(' ')[5];

                for(int i=0;i< noteString.Length;i++)
                {
                    noteString[i] = Convert.ToInt32(noteString[i]);

                string[] notesString
                }



                Student s = new Student(cnp, nume, nrmat, datainrol, nrRest, note);
                tb_CitireDinTxt.Text += s.ToString() + Environment.NewLine;

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

如何从文件中读取矢量注?谢谢

更新:

private void button_CitireTXT_Click(object sender,EventArgs e){

StreamReader sr = new StreamReader("citiretxt.txt");
        string linie = null;
        while ((linie = sr.ReadLine()) != null)
        {
            try
            {
                string cnp = linie.Trim().Split(',')[0];
                string nume = linie.Trim().Split(',')[1];
                int nrmat = Int32.Parse(linie.Trim().Split(',')[2]);
                string datainrolString = linie.Trim().Split(',')[3];
                DateTime datainrol = DateTime.Parse(datainrolString);
                int nrRest = Int32.Parse(linie.Trim().Split(',')[4]);

               int[] notes = Array.ConvertAll(linie.Split(',')[5].Split(' '), int.Parse);


                Student s1 = new Student(cnp, nume, nrmat, datainrol, nrRest, notes);

                tb_CitireDinTxt.Text += s1.ToString() + Environment.NewLine;

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

1 Answer

  • 0

    Writing answer by considering, Vector(from C++) -> List(in C#).

    如果以下是您的文件内容,

    2920118080023, George, 2, 12/09/2016, 4, 10 3 4
    

    , 拆分 . 那么你在第5个索引处的字符串将包含 10 3 4 . 现在将它转换为整数数组使用 Array.ConvertAll() 或者你也可以将它转换为List

    尝试下面的代码

    //Converting into int array
    int[] notes = Array.ConvertAll(linie.Split(',')[5].Split(' '), int.Parse)
    
    //Converting into List<int>
    List<int> notes= Array.ConvertAll(linie.Split(',')[5].Split(' '), int.Parse).ToList()
    

    代替

    string noteString = linie.Split(' ')[5];
    
    for(int i=0;i< noteString.Length;i++)
         {
              noteString[i] = Convert.ToInt32(noteString[i]);
    
              string[] notesString
         }
    

Related