Piszę prostą metodę, która powinna odczytywać dane z pliku tekstowego. Nie rozumiem, dlaczego ta metoda odczytuje tylko 2, 4, 6 linijek? Metoda jest poniżej. Co jest nie tak w moim kodzie?

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;
    }
0
abilash 25 wrzesień 2012, 03:04

2 odpowiedzi

Najlepsza odpowiedź

Dzwonisz do line = sr.ReadLine() dwa razy.

Usuń tę linię, string str = sr.ReadLine(); i użyj zmiennej line

5
Luke Hutton 25 wrzesień 2012, 03:05

To powinno wyglądać tak:

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;
}
0
Chris 25 wrzesień 2012, 03:16