How To Read Data Into A List Structure in C#? -


i writing program uses studentstruct (structure) consists of: studentid, studentname, , grades (which separate list). program supposed read in sequential text file , contains methods allow user to: add student, add grades (to particular student), change student name, , delete student. @ end of program supposed overwrite previous file new changes made current program session.

my question how read in data text file separate structure variables within lists?

my text file looks this:

00000,mimzi dagger,100,50,75,70,45,10,98,83 00001,alexander druaga,89,45,80,90,15,73,99,100,61 00002,nicholas zarcoffsky,100,50,80,50,75,100,100 00003,kantmiss evershot,50,100 

once structure variables within list filled, how overwrite file contents of list structure in same format above file?

since have multiple , varying amounts of grades, how achieve looping through , adding each grade grades list?

as can maybe tell, new c# , first project. appreciated!

    class program     {         static string pathsource = @"c:\schoolfiles\studentgrades.txt";          struct studentstruct         {             public string studentid;             public string studentname;             public list<string> grades;         }          static void main(string[] args)         {             studentstruct student = new studentstruct();             list<studentstruct> mylist = new list<studentstruct>();             student.grades = new list<string>();         } 

update: here have come far:

for (int = 0; < filelines.length; i++) {     output = filelines[i];     string[] outputarray = output.split(',');      student.grades = new list<string>();     student.studentid = outputarray[0];     student.studentname = outputarray[1];      (int j = 2; j < outputarray.length; j++)     {         student.grades.add(outputarray[j]);     }      mylist.add(student); 

update: code above worked out wonderfully. here top part of code involving question:

static void main(string[] args)     {         //declare variables         string output;          //read file array         string[] filelines = file.readalllines(pathsource);          studentstruct student = new studentstruct();         list<studentstruct> mylist = new list<studentstruct>();            (int = 0; < filelines.length; i++)         {             output = filelines[i];             string[] outputarray = output.split(',');              student.grades = new list<string>();             student.studentid = outputarray[0];             student.studentname = outputarray[1];              (int j = 2; j < outputarray.length; j++)             {                 student.grades.add(outputarray[j]);             }              mylist.add(student);         }          mainmenu(mylist);     } 

and add list file did this:

static void exitmodule(list<studentstruct> mylist)     {         //declare variables         string inputchoice = null;         string output = null;          system.io.streamwriter file = new system.io.streamwriter(pathsource);          console.clear();         console.writeline("are sure want exit program? y/n");         inputchoice = console.readline();          if (inputchoice == "y" || inputchoice == "y")         {             (int = 0; < mylist.count; i++)             {                 output = (mylist[i].studentid + "," + mylist[i].studentname);                  (int j = 0; j < mylist[i].grades.count; j++)                 {                     output += ("," + mylist[i].grades[j]);                 }                  file.writeline(output);             }              file.close();             environment.exit(0); 

since homework, may time learn data structures. store information you've read in text file efficiently, allowing write out more after modifying records.

here couple of other random pointers:

  • the system.io.file.readlines(pathsource) method place start reading each line of text file
  • you should never using struct in c# (especially 1 mutable in example) unless well-versed in semantics , purpose. use class instead

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -