0

I have a text file of student which i have to read and save it in the array list. The formal of the file is fist name, second name, marks and each name is written in a new line kindly help me out how to do it File format:

First Name
Last Name
Marks
First Name
Last Name
Marks
First Name
Last Name
Marks

Here's what I've tried so far:

List<string> fileContent = new List<string>(); 
TextReader tr = new StreamReader("A.txt"); 
string currentLine = string.Empty; 
while ((currentLine = tr.ReadLine()) != null) 
{ 
    fileContent.Add(currentLine); 
} 
5
  • 1
    Any reason for using ArrayList at all rather than a List<T>? Aside from that, what have you already tried, and what went wrong? We're not going to just do your homework for you, but we'll help you work out what's wrong with your attempt. Commented Sep 2, 2013 at 20:28
  • common its not my homework i am working for a commpany and this is the only problem i am facing right now i am not able to figure out how to do it list would be fine but i am not sure how to do it i have tried this Commented Sep 2, 2013 at 20:55
  • List<string> fileContent = new List<string>(); TextReader tr = new StreamReader("A.txt"); string currentLine = string.Empty; while ((currentLine = tr.ReadLine()) != null) { fileContent.Add(currentLine); } Commented Sep 2, 2013 at 20:56
  • List<string> fileContent = new List<string>(); TextReader tr = new StreamReader("A.txt"); string currentLine = string.Empty; while ((currentLine = tr.ReadLine()) != null) { fileContent.Add(currentLine); } Commented Sep 2, 2013 at 20:57
  • Edit the question with this as source code look ugly in comments Commented Sep 2, 2013 at 21:08

2 Answers 2

1

Below's an example of reading a file of the format you specify and pushing the results into a List (or ArrayList if you prefer) of People. Based on this you should be able to create a list of strings if that's your preferences, though I'd suspect you'd want a list of people?

class Program
{
    static void Main(string[] args)
    {
        string fn = @"c:\myfile.txt";
        IList list = new ArrayList();
        FileReader(fn, ref list);
        for (int i = 0; i < list.Count; i++)
        {
            Console.WriteLine(list[i].ToString());
        }
        Console.ReadKey();
    }
    public static void FileReader(string filename, ref IList result)
    {
        using (StreamReader sr = new StreamReader(filename))
        {
            string firstName;
            string lastName;
            string marks;
            IgnoreHeaderRows(sr);
            while (!sr.EndOfStream)
            {
                firstName = sr.EndOfStream ? string.Empty : sr.ReadLine();
                lastName = sr.EndOfStream ? string.Empty : sr.ReadLine();
                marks = sr.EndOfStream ? string.Empty : sr.ReadLine();
                result.Add(new Person(firstName, lastName, marks));
            }
        }
    }
    const int HeaderRows = 2;
    public void IgnoreHeaderRows(StreamReader sr)
    {
        for(int i = 0; i<HeaderRows; i++)
        {
            if(!sr.EndOfStream) sr.ReadLine();
        }
    }
}

public class Person
{
    string firstName;
    string lastName;
    int marks;
    public Person(string firstName, string lastName, string marks)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        if (!int.TryParse(marks, out this.marks))
        {
            throw new InvalidCastException(string.Format("Value '{0}' provided for marks is not convertible to type int.", marks));
        }
    }
    public override string ToString()
    {
        return string.Format("{0} {1}: {2}", this.firstName, this.lastName, this.marks);
    }
    public override int GetHashCode()
    {
        return this.ToString().GetHashCode();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks John this is a perfect solution but i have one more question if if have to skip first two lines forexample in my textfile, the first two lines are general information which i dont want to extract i have to start from the 3rd line .
No worries - the simplest way would be to read them and ignore; i.e. int headerRows = 2; for(i=0;i<headerRows;i++) if(!sr.EndOfStream) sr.ReadLine();
0

JohnLBevan - To call IgnoreHeaderRows in FileReader, we need to change IgnoreHeaderRows to static as non-static members cannot be accessed in a static method. Correct me if I'm wrong.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.