0

I'm writing simple method that should read data from the text file.I can't understand why this method read only 2nd, 4th, 6 lines? Method is below. What is wrong in my code?

public static List<Employee> ReadFromFile(string path = "1.txt") 
    {
        List<Employee> employees = new List<Employee>();
        Stream stream = null;
        StreamReader sr = null;
        try
        {
            stream = new FileStream(path, FileMode.Open, FileAccess.Read);
            stream.Seek(0, SeekOrigin.Begin);
            sr = new StreamReader(stream);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                Employee employee = new DynamicEmployee();
                string str = sr.ReadLine();
                employee.FirstName = str.Substring(1, 20).Trim();
                employee.LasttName = str.Substring(20, 20).Trim();
                employee.Paynment = Convert.ToDouble(str.Substring(40, 20).Trim());
                Console.WriteLine("{0} {1} {2}", employee.FirstName, employee.LasttName, employee.Paynment);
                employees.Add(employee);
                //Console.WriteLine(str);
            }
        }
        catch//(System.FormatException)
        {
            Console.WriteLine("File format is incorect");
        }
        finally
        {
            sr.Close();
            stream.Close();
        }
        return employees;
    }

2 Answers 2

5

You are calling line = sr.ReadLine() twice.

Remove this line, string str = sr.ReadLine(); and use variable line

Sign up to request clarification or add additional context in comments.

Comments

0

It should look like this:

public static List<Employee> ReadFromFile(string path = "1.txt") 
{
    List<Employee> employees = new List<Employee>();
    Stream stream = null;
    StreamReader sr = null;
    try
    {
        stream = new FileStream(path, FileMode.Open, FileAccess.Read);
        stream.Seek(0, SeekOrigin.Begin);
        sr = new StreamReader(stream);
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            Employee employee = new DynamicEmployee();
            // string str = sr.ReadLine(); // WRONG, reading 2x
            employee.FirstName = line.Substring(1, 20).Trim();
            employee.LasttName = line.Substring(20, 20).Trim();
            employee.Paynment = Convert.ToDouble(line.Substring(40, 20).Trim());
            Console.WriteLine("{0} {1} {2}", employee.FirstName, employee.LasttName, employee.Paynment);
            employees.Add(employee);
            //Console.WriteLine(str);
        }
    }
    catch//(System.FormatException)
    {
        Console.WriteLine("File format is incorect");
    }
    finally
    {
        sr.Close();
        stream.Close();
    }
    return employees;
}

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.