Search code examples
c#visual-studiofilereadfilewindows-forms-designer

C# Read .txt file, store its data and edit the file


I am trying to do something pretty simple. I am making a Windows Form Application, it's a simple 'Student grade calculator' essentially. I have the form working, it can read the file and show its data in a text box. However, I need to store each line's columns in their own fields.

You can see below an example of a file it should read/edit/save.

enter image description here

This is what I currently have that I use to read in a file:

      private void LoadFile()
    {
        string lineFromFile;

        fileContentTextBox.Clear();

        try
        {
            using (StreamReader reader = new StreamReader(fileName))
            {
                while (!reader.EndOfStream)
                {
                    lineFromFile = reader.ReadLine();

                    fileContentTextBox.AppendText(lineFromFile);

                    fileContentTextBox.AppendText(Environment.NewLine);
                }
            }

So, how would I be able to store its data in fields such as:

  • moduleCode1 "SOFT152"
  • examWeighting1 "0.3"
  • courseworkeMark1 "65"
  • etc..

I know that you have to use something like this, but I'm not sure how to use it in this situation where I need to store the file's data in many individual fields?

    lines[i].Split(',')

The output in the form would look something like this in the end:

enter image description here

If there is a better way of doing this, like taking each line into a string and then separating it or something please let me know.


Solution

  • I can't describe with code as I'm on my phone, but I would do something like this:

    Create a new class, call it student if you want. Inside student, create the properties you require (eg mark, weighing).

    In your main program, creating a new list of student.

    In your while loop, where you read the line, create a new student. Then, split the line into your string array. Access the string array by index, grab your properties and assign the value to the student properties.

    Finally, add the created student to the student list.

    UPDATE with some code

    Okay, so let's imagine that you were creating an address book for students.

    You would have a Student class:

    public class Student
    {
         public string Name {get;set;}
         public int Age {get;set;}
    }
    

    Then in your main program, you want to create a list to store your students:

    var students = new List<Student>();
    

    Finally, you want to read the file, create your student and add him/her to the list:

    while (!reader.EndOfStream)
    {
         var student = new Student();
         ineFromFile = reader.ReadLine();
         var arrayOfProperties = ineFromFile.Split();
         student.Name = arrayOfProperties[0]; #Make sure you know the indices, or you will have to create a custom parser ;)
         student.Age = (int)arrayOfProperties[1]; #Remember to convert from string.
         students.Add(student); # add your student!
    }