Search code examples
c#visual-studiofiletextstreamreader

Print from text file to console using a selected method


So i can't seem to find anything about this online. So i'm wanting to know how to print data from a text file to the console application through a method.

Here's an example, This is the class Student, and the method I am wanting to use as a type of layout is DisplayInfo():

class Student
{
    public string ID { get; set; }
    public double Score { get; set; }

    public Student(string SID, double SC)
    {
        ID = SID;
        Score = SC;
    }

    public void DisplayInfo()
    {
        Console.WriteLine(ID,": " + Score);
    }
}

Here is what i've got so far inside my PrintData void:

public void PrintData()
{
    using (StreamReader readtext = new StreamReader("data.txt"))
    {
        string readMeText = readtext.ReadLine();

    }
}

Basically, again, I am wanting to print it in the format shown in DisplayInfo(), inside the PrintData() void.


Solution

  • Let's assume that you have text (demo.txt) file in this format :

    s1 140
    s2 250
    s3 400

    Where s1,s2,s3 are studeintID and 140,250,400 are their scores.

    Now you need read lines from file while it is not the End of Stream split read line by whitespace and write to console. The following function is doing right that:

    public void PrintData()
    {
        using (var readtext = new StreamReader(@"c:\Users\IIG\Desktop\demo.txt"))
        {
            while (!readtext.EndOfStream)
            {
                string currentLine = readtext.ReadLine();
                var args = currentLine.Split(' ');
                if (args.Length > 1)
                {
                    Console.WriteLine(args[0] + ":" + args[1]);
                }
                else
                {
                    Console.WriteLine("Invalid line");
                }
            }
        }
    }
    

    UPDATE

    Here is example how you can use object serialization/deserialization for this:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml.Serialization;
    
    namespace ConsoleApplication4
    {
        public class Program
        {
            static void Main(string[] args)
            {
                List<Student> students = new List<Student>();
                students.Add(new Student("s1", 140));
                students.Add(new Student("s2", 200));
                students.Add(new Student("s3", 250));
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Student>));
                if(!File.Exists(@"c:\Users\IIG\Desktop\demo.txt"))
                {
                    using (File.Create(@"c:\Users\IIG\Desktop\demo.txt"))
                    {
    
                    }
    
                }
                using (var writer = new StreamWriter(@"c:\Users\IIG\Desktop\demo.txt"))
                {
                    xmlSerializer.Serialize(writer,students);
                }
                PrintData();
            }
            [Serializable]
            public class Student
            {
                public string ID { get; set; }
                public double Score { get; set; }
    
                public Student()
                {
    
                }
                public Student(string SID, double SC)
                {
                    ID = SID;
                    Score = SC;
                }
                public void DisplayInfo()
                {
                    Console.WriteLine(ID+ ": " + Score);
                }
            }
            public static void PrintData()
            {
                using (var readtext = new StreamReader(@"c:\Users\IIG\Desktop\demo.txt"))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Student>));
                    List<Student> readStudents = (List<Student>)xmlSerializer.Deserialize(readtext);
                    foreach (var student in readStudents)
                    {
                        student.DisplayInfo();
                    }
    
                }
            }
        }
    }