oop - I can't Read from FileText C# -
i have make connection between students,classes, , year this: 1 year can have 1 or more classes , 1 class can have 1 or more students.
i made generic list. problem have information 1 .txt
file , don't know how it.
my file this:
(year,class,name,surname,average). 1 314 george andrew 8 2 324 popescu andrei 9 2 323 andreescu bogdan 10 3 332 marin darius 9 3 332 constantin roxana 10
code:
public class student { public string name { get; set; } public string surname { get; set; } public int average { get; set; } } } public class grupa { public int name { get; set; } public list<student> setstudent { get; set; } public grupa() { setstudent = new list<student>(); } public void print() { //console.writeline("grupa: " + this.name); console.writeline("studentii din grupa: "); foreach (student s in this.setstudent) { console.writeline(" " + s.name+ " " + s.surname + " --- " + s.average+"\n"); } } } public class { public int anul { get; set; } public list<grupa> setgrupa { get; set; } public an() { setgrupa = new list<grupa>(); } public void print() { console.writeline("anul: " + this.anul); console.writeline("grupele din acest an: "); foreach (grupa g in this.setgrupa) { console.writeline(" " + g.name); } } } string[] lines = system.io.file.readalllines(@"d:\c#\tema1\tema1.txt"); system.console.writeline("content tema1.txt= \n"); foreach (string line in lines) { console.writeline("\t" + line); } console.writeline("\n close"); system.console.readkey(); }
you can use .net textfieldparser
type of flat file:
var studentlist = new list<student>(); var parser = new microsoft.visualbasic.fileio.textfieldparser("<file path>"); parser.setfieldwidths(4, 4, 12, 8, 2); while (!parser.endofdata) { string[] line = parser.readfields(); var student = new student(); student.year = int.parse(line[0]); student.class = int.parse(line[1]); student.name = line[2].trim(); student.surname = line[3].trim(); student.average = int.parse(line[4]); studentlist.add(student); }
you have setup field lengths in setfieldwidths
function.
Comments
Post a Comment