November 18th, 2012 07:34 AM
#1
Writng a list to a text file
Hi I have the following code which adds student details and their marks to a list and then save it to a text file. I am not implementing the WriteAll() method correctly. Could someone help me in implementing it? Thanks. Here is the code:
[code]
using System.Linq;
using System.Text;
namespace Main
{
public class Student
{
private string name;
private string surname;
private int dob;
private string address;
private string id;
public Student()
{
//empty contructor
}
public Student(string name, string surname, int dob, string address, string id)
{
this.name = name;
this.surname = surname;
this.dob = dob;
this.address = address;
this.id = id;
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Surname
{
get { return surname; }
set { surname = value; }
}
public int DOB
{
get { return dob; }
set { dob = value; }
}
public string Addr
{
get { return address; }
set { address = value; }
}
public string Id
{
get { return id; }
set { id = value; }
}
}
}
public struct Marks
{
private int hyexam;
private int anexam;
public int HYEXAM
{
get { return hyexam; }
set { hyexam = value; }
}
public int ANEXAM
{
get { return anexam; }
set { anexam = value; }
}
public Marks(int hyexam, int anexam)
{
this.hyexam = hyexam;
this.anexam = anexam;
}
public double OverallExam()
{
return (0.4 * hyexam) + (0.6 * anexam);
}
}
public class JuniorStudents : Student
{
public int numofStudents;
public Marks Mathematics;
public Marks English;
public Marks Maltese;
public Marks ReligiousStudies;
public Marks SocialStudies;
public string filename = "C:\\Users\\Melvic\\Desktop\\Students";
public List<Object> studentList = new List<Object>();
public int ReadAllRecords()
{
studentList.Clear();
numofStudents = 0;
try {
System.IO.Stream s1 = System.IO.File.Open(filename, System.IO.FileMode.Open);
System.IO.BinaryReader f1 = new System.IO.BinaryReader(s1);
//string s;
// StreamReader myFile = File.OpenText("JuniorStudents.txt");
// while ((s = myFile.ReadLine()) != null)
// {
// Console.WriteLine(s);
// }
// myFile.Close();
numofStudents = f1.ReadInt16();
for (int i = 0; i < numofStudents; i++)
{
Student stud = new Student();
stud.Name = f1.ReadString();
stud.Surname=f1.ReadString();
stud.DOB=f1.ReadInt16();
stud.Addr=f1.ReadString();
stud.Id=f1.ReadString();
Mathematics.HYEXAM = f1.ReadInt16();
Mathematics.ANEXAM = f1.ReadInt16();
English.HYEXAM = f1.ReadInt16();
English.ANEXAM = f1.ReadInt16();
Maltese.HYEXAM= f1.ReadInt16();
Maltese.ANEXAM = f1.ReadInt16();
ReligiousStudies.HYEXAM = f1.ReadInt16();
ReligiousStudies.ANEXAM= f1.ReadInt16();
SocialStudies.HYEXAM = f1.ReadInt16();
SocialStudies.ANEXAM = f1.ReadInt16();
//for (int j = 0; j < 5; j++) {
// stud.Marks[j] = f1.ReadInt16();
//}
studentList.Add(stud);
studentList.Add(Mathematics);
studentList.Add(English);
studentList.Add(Maltese);
studentList.Add(ReligiousStudies);
studentList.Add(SocialStudies);
}
f1.Close();
}
catch (FileNotFoundException e){
Console.WriteLine (e.ToString());
}
return numofStudents;
}
public int WriteAllRecords()
{
Student stud = new Student();
StreamWriter myFile = File.CreateText("Juniorstudents.txt");
numofStudents = studentList.Count;
myFile.Write(numofStudents);
myFile.Close();
for(int i = 0; i < numofStudents; i++)
{
foreach (Student stu in studentList)
{
myFile.Write(stu.Name);
myFile.Write(stu.Surname);
myFile.Write(stu.Addr);
myFile.Write(stu.DOB);
myFile.Write(stu.DOB);
}
foreach (Marks mks in studentList)
{
myFile.Write(Mathematics.HYEXAM);
myFile.Write(Mathematics.ANEXAM);
myFile.Write(English.HYEXAM);
myFile.Write(English.ANEXAM);
myFile.Write(Maltese.HYEXAM);
myFile.Write(Maltese.ANEXAM);
myFile.Write(ReligiousStudies.HYEXAM);
myFile.Write(ReligiousStudies.ANEXAM);
myFile.Write(SocialStudies.HYEXAM);
myFile.Write(SocialStudies.ANEXAM);
}
}
myFile.Close();
return numofStudents;
}
public int AddRecord(string name, string surname, int dob, string address, string id,int hyexam, int anexam)
{
Student stud = new Student();
bool error=false;
if (error==false) {
try {
stud.Name=name;
stud.Surname=surname;
stud.DOB=dob;
stud.Addr=address;
stud.Id=id;
Mathematics.HYEXAM = hyexam;
Mathematics.ANEXAM = anexam;
English.HYEXAM = hyexam;
English.ANEXAM = anexam;
Maltese.HYEXAM = hyexam;
Maltese.ANEXAM = anexam;
ReligiousStudies.HYEXAM = hyexam;
ReligiousStudies.ANEXAM= anexam;
SocialStudies.HYEXAM = hyexam;
SocialStudies.ANEXAM = anexam;
}
catch(FormatException ex) {
error=true;
Console.WriteLine (ex.ToString());
}
StreamWriter myFile = File.AppendText("JuniorStudents.txt");
studentList.Add(stud);
myFile.WriteLine(studentList);
numofStudents = studentList.Count;
}
return 1;
}
public int EditRecord(int index, string name, string surname, int dob, string addr, string id, int hyexam, int anexam)
{
Student stud = new Student();
bool error=false;
if (error==false) {
try {
stud.Name = name;
stud.Surname=surname;
stud.DOB=dob;
stud.Addr=addr;
stud.Id=id;
Mathematics.HYEXAM= hyexam;
Mathematics.ANEXAM= anexam;
English.HYEXAM = hyexam;
English.ANEXAM = anexam;
Maltese.HYEXAM = hyexam;
Maltese.ANEXAM = hyexam;
ReligiousStudies.HYEXAM = hyexam;
ReligiousStudies.ANEXAM= anexam;
SocialStudies.HYEXAM= hyexam;
SocialStudies.ANEXAM = anexam;
}
catch (FormatException ex){
error=true;
Console.WriteLine (ex.ToString());
}
studentList[index] = stud;
numofStudents = studentList.Count;
StreamWriter myFile = File.AppendText("JuniorStudents.txt");
studentList.Add(stud);
myFile.WriteLine(studentList);
}
return 1;
}
public int DeleteRecord(int index)
{
studentList.RemoveAt(index);
WriteAllRecords();
return 1;
}
}
}
[\code]
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks