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]
Re: Writng a list to a text file
Remove the first myFile.Close().
Code:
public int WriteAllRecords()
{
Student stud = new Student();
StreamWriter myFile = File.CreateText("Juniorstudents.txt");
numofStudents = studentList.Count;
myFile.Write(numofStudents);
myFile.Close(); // <-- Remove this one
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;
}
Once you/ve closed the file, you can't write to it anymore unless you re-open it.
Re: Writng a list to a text file
Ok thanks but do you think the Write method is implemented correctly?
Re: Writng a list to a text file
Ok, if I understand correctly what you are trying to do...
The code as you have it written will output the number of students, then the full list of students, then the full list of marks, and it will repeat the list of students and list of marks numOfStudents times.
I assume what you want to do is output for each student in the list, the particulars for that student and their marks , then repeat for the next student until all students have been output.
First, I would make Marks a member of Student (implemented as a IList<Marks>).
Then all you need to do is loop through the list of Student objects, and for each member of the list, output the particulars, then output all that student's marks.
You do not need the for loop, and if you make a list of marks part of the Student object, you will have an outer foreach "Student objects" and an inner foreach "Marks objects for the current Student object".
Hope this makes sense.
Re: Writng a list to a text file
Also I am getting an authorize access exception in the Read method. Do you know why this is so?
Re: Writng a list to a text file
Yes that is what I wanted to do. Can you please give me an example of how can I do it because I didn't understand well with words
Re: Writng a list to a text file
An authorization exception normally means that the user account that the code is running under does not have sufficient permissions to access the file. Also, why are you using a binary reader to read a text file?