CLR: .Net / Mono / Boo / Otros CLR > C#
Lectura De Ficheros
(1/1)
usu2:
Hola,
Estoy trabajando en una aplicación que lee de un fichero de texto. El problema es que hay veces que lee una linea y al pasar a leer la siguiente, la cadena en la que recojo el resultado se queda vacia, aun habiendo lineas en el fichero.
Gracias por todo de antemano, :kicking:
El codigo es el siguiente:
--- Código: Text --- using System;using System.Data;using System.Data.SqlClient;using System.IO;using System.Collections; namespace prueba{ public class ConfirmacionEntradas { static void Main() {ConfirmacionEntradas entrada = new ConfirmacionEntradas(); entrada.leer(); } /*Cogemos un string y lo separamos en partes y los metemos en un arraylist. * Dependiendo de las primeras 4 letras de cada linea, aplicamos un formato u otro*/ public ArrayList separa(string buffer) { ArrayList lista = new ArrayList(); string tipoRegistro = buffer.Substring(0,4); if (tipoRegistro == "CECA") { tipoRegistro = buffer.Substring(4,20); lista.Add(tipoRegistro); tipoRegistro = buffer.Substring(24,20); lista.Add(tipoRegistro); tipoRegistro = buffer.Substring(44,20); lista.Add(tipoRegistro); tipoRegistro = buffer.Substring(64,3); lista.Add(tipoRegistro); tipoRegistro = buffer.Substring(67,12); lista.Add(tipoRegistro); tipoRegistro = buffer.Substring(79,20); lista.Add(tipoRegistro); tipoRegistro = buffer.Substring(99,50); lista.Add(tipoRegistro); tipoRegistro = buffer.Substring(149,50); lista.Add(tipoRegistro); tipoRegistro = buffer.Substring(199,50); lista.Add(tipoRegistro); } else { tipoRegistro = buffer.Substring(4,20); lista.Add(tipoRegistro); tipoRegistro = buffer.Substring(24,20); lista.Add(tipoRegistro); tipoRegistro = buffer.Substring(44,7); lista.Add(tipoRegistro); tipoRegistro = buffer.Substring(51,7); lista.Add(tipoRegistro); tipoRegistro = buffer.Substring(58,11); lista.Add(tipoRegistro); } return lista; } /*Leemos de un fichero y luego visualizamos el arraylist devuelto por la funcion separa*/ public void leer() { StreamReader lector = File.OpenText("COE.7"); string buffer; ArrayList devuelto = new ArrayList(); buffer = lector.ReadLine(); while (buffer != "") { Console.WriteLine("Buffer: {0}",buffer); devuelto = separa(buffer); for (int i = 0;i < devuelto.Count;i++) Console.WriteLine(devuelto[i].ToString()); devuelto.Clear(); buffer = lector.ReadLine(); } lector.Close(); Console.ReadLine(); } } }
sergiotarrillo:
como quieres tu!, leer el archivo caracter por caracter o linea por linea?
Navegación
Ir a la versión completa