C# 在C语言中显示和/或存储文本文件内容的问题#

C# 在C语言中显示和/或存储文本文件内容的问题#,c#,C#,对于我的中间C#类,我的任务是提取和维护通过文件流连接到程序的.txt文件中的数据。我对编码的所有理解都是正确的,但当表单加载时,它不会提取数据。我已经从我能想到的各个角度对它进行了麻烦处理,但我对我现在的处境感到茫然。我将在下面介绍相关的代码,但我有一个类,其中包含专门用于处理文件流和数据填充的方法,还有一个用于“学生对象”的类。感谢您的帮助 这是我试图读取的.txt文件中的数据 杰夫·迪克森| 100 | 97 | 68 Sharon Beaudry | 95 | 76 | 87 Halli

对于我的中间C#类,我的任务是提取和维护通过文件流连接到程序的.txt文件中的数据。我对编码的所有理解都是正确的,但当表单加载时,它不会提取数据。我已经从我能想到的各个角度对它进行了麻烦处理,但我对我现在的处境感到茫然。我将在下面介绍相关的代码,但我有一个类,其中包含专门用于处理文件流和数据填充的方法,还有一个用于“学生对象”的类。感谢您的帮助

这是我试图读取的.txt文件中的数据

杰夫·迪克森| 100 | 97 | 68

Sharon Beaudry | 95 | 76 | 87

Hallie Neupert | 95 | 89 | 94

下面是我在StudentsDB类中从源文件中提取的方法

public static List<Student> GetStudents()
        {
            //Creates a new list to be returned
            List<Student> students = new List<Student>();
            //Creates a new file stream to read the data
            StreamReader filler = new StreamReader(new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));
            //Starts the fill loop
            while(filler.Peek() != -1)
            {
                string row = filler.ReadLine();
                //Creates an array to fill with the line, deliniated by the pipe
                string[] columns = row.Split('|');
                //Creates an instantiation of the Student Class to use for filling purposes
                Student student = new Student();
                //Adds the student name from the array to the Student Object
                student.Name = columns[0];
                //Creates a loop that will go through the string array and pull the scores to add to the list
                //Converting them to integer in the process
                for (int i = 1; i < columns.Length; i++)
                {
                    student.Scores.Add(Convert.ToInt32(columns[i]));
                }
                //Adds the instance of the student object to the list
                students.Add(student);
            }
            //Closes the stream and returns the list 
            filler.Close();
            return students;
publicstaticlist GetStudents()
{
//创建要返回的新列表
列出学生=新建列表();
//创建新的文件流以读取数据
StreamReader filler=新的StreamReader(新的文件流(路径,FileMode.OpenOrCreate,FileAccess.Read));
//开始填充循环
while(filler.Peek()!=-1)
{
字符串行=filler.ReadLine();
//创建一个数组以填充由管道删除的线
string[]columns=row.Split(“|”);
//创建学生类的实例化以用于填充
学生=新生();
//将数组中的学生名称添加到学生对象
student.Name=列[0];
//创建一个循环,该循环将遍历字符串数组并将分数添加到列表中
//在此过程中将它们转换为整数
for(int i=1;i
这是一个包含属性和方法的学生类:

public class Student
    {
        //General Constructor
        public Student()
        { }

        //Public Properties
        public string Name
        {
            get;
            set;
        }
        public List<int> Scores
        {
            get;
            set;
        }

        public string GetDisplayText()
        {
            string total = "";
            foreach (int score in Scores)
            {
                total += "|" + score;
            }
            return Name + total;
        }


    }
公共班级学生
{
//总建造师
公立学生()
{ }
//公共财产
公共字符串名
{
收到
设置
}
公开名单分数
{
收到
设置
}
公共字符串GetDisplayText()
{
字符串总数=”;
foreach(分数中的整数分数)
{
总分+=“|”+分;
}
返回名称+总数;
}
}
下面是我用每个学生项目填充列表框的方法:

//Program wide variable for a list of students
        public List<Student> students = null;

        //Method to fill the list box
        private void FillBox()
        {
            //Clears the list box in order to allow for the data to be entered
            lstStudents.Items.Clear();
            //Fills the students list variable using the method from studentDB class
            students = studentDB.GetStudents();
            //Cycles throught the list and fills the list box
            foreach(Student s in students)
            {
                lstStudents.Items.Add(s.GetDisplayText());
            }
        }
//学生列表的程序范围变量
公共名单学生=null;
//方法来填充列表框
私有void FillBox()
{
//清除列表框以允许输入数据
lstStudents.Items.Clear();
//使用studentDB类中的方法填充students列表变量
students=studentDB.GetStudents();
//循环浏览列表并填充列表框
foreach(学生中的学生)
{
lststudens.Items.Add(s.GetDisplayText());
}
}
再次感谢您的帮助。

  • 您没有初始化
    Student。构造函数中的分数
    ,因此它将为
    null
    。您不能将项添加到
    null
  • 检查
    .ReadLine()
    是否返回
    null
    break
    ,而不是
    Peek
    读取器,会更容易
  • 您应该为
    StreamReader
    使用
    using()
    块,而不是手动管理
    .Close()

除此之外,一切似乎都正常。

我还没有测试过这一点,但以前在访问字符串数组中的数据时遇到过问题。在GetStudents()函数中,您有:

 for (int i = 1; i < columns.Length; i++)

 {

     student.Scores.Add(Convert.ToInt32(columns[i]));
 }
for(int i=1;i
当我遇到类似问题时,我做到了:

 for (int i = 1; i < columns.Length -1; i++)

 {

     student.Scores.Add(Convert.ToInt32(columns[i]));
 }
for(int i=1;i
在大多数编程语言中,数组从0开始。如果有一个包含3个元素的数组,则有3个位置可用-位置0、1和2。因此,获取长度将返回3,因此需要减去1以获取最后一个元素,即位置2处的元素。


请让我知道这是否有帮助!

您好,您能指定“它没有提取数据”吗?;)有什么问题吗?您的文件流不工作吗?您是否有异常/错误?或者是否加载了所有内容,但您的表单没有显示预期的内容?我不确定断开连接的位置。我不认为问题出在文件流上,好像我将lststudens.Items设置为仅显示列表一样。tostring()它显示“Collection”。我认为问题在于我如何将数据解析到Students对象中。当我回到本作业时,我肯定会尝试这样做。这不会有帮助-事实上,这会引入一个错误,学生的最后分数没有读入。那么列表属性在类中设置时的行为是否与其他属性不同呢?请看下面的内容你的回答这实际上可能是问题所在,今天晚些时候回到作业时我会试试这个。这样做了。添加了显式构造函数完成了这项工作。如果我为文件添加文件扩展名,这也会有所帮助。