Hola,
Estoy desarrollando una app que lea un fichero con la forma:
N:Nombre;D:0000;F:00/00/0000
Donde N: es un string, D: es un entero y F: es una fecha. (cabe destacar que el orden puede estar alterado y puede que alguna linea no esté bien escrita)
El problema reside en que el método en el cual almaceno los contenidos de la línea en una lista, al llegar al segundo ';' se revoluciona y hace cosas extrañas.
Código Delphi
[-]procedure TForm1.CreateList(fileName: string);
var
tempLine: string; line: TLine; cha: char;
Data: TextFile;
tipe: string; temp: string; nameData: string; numberData: integer; dateData: TDateTime;
lineData: integer;
begin
fileList := TList.Create;
wrongLines := 0;
rightLines := 0;
lineData := 1;
AssignFile(Data, fileName);
Reset(Data);
while not Eof(Data) do
begin
while not Eoln(Data) do
begin
Read(Data, cha);
if UpperCase(temp) = 'N:'
then
begin
tipe := temp;
temp := '';
end
else if UpperCase(temp) = 'D:'
then
begin
tipe := temp;
temp := '';
end
else if UpperCase(temp) = 'F:'
then
begin
tipe := temp;
temp := '';
end;
if (cha = ';') or (lineData = 3)
then
begin
if tipe = 'N:'
then
begin
try
nameData := temp;
temp:='';
except
Inc(wrongLines);
temp:='';
break;
end;
end
else if tipe = 'D:'
then
begin
try
numberData := StrToInt(temp);
temp:='';
except
Inc(wrongLines);
temp:='';
break;
end;
end
else if tipe = 'F:'
then
begin
try
dateData := StrToDate(temp);
temp:='';
except
Inc(wrongLines);
temp:='';
break;
end;
end
else
ShowMessage(IntToStr(lineData));
Inc(lineData);
end
else temp := temp + cha;
ShowMessage('Tipo: ' + tipe + #13 + 'Temp: ' + temp + #13 + 'Caracter: ' + cha
+ #13 + 'N: ' + nameData + #13 + 'D: ' + IntToStr(numberData) + #13 +
'F: ' + DateToStr(dateData));
end;
if not (nameData = '')
then
begin
if not (numberData = 0)
then
begin
if not (dateData = 0)
then
begin
line := TLine.Create(nameData, numberData, dateData);
Inc(rightLines);
fileList.Add(line);
end;
end;
end;
nameData := '';
numberData := 0;
dateData := 0;
temp := '';
lineData := 1;
ReadLn(Data,tempLine);
end;
CloseFile(Data);
end;
TList es una clase creada para que me diferencie qué es cada cosa
Código Delphi
[-]TLine = class
private
n: string; d: integer; f: TDateTime;
public
line : string;
property NewName: string
read n;
property NewNumber: integer
read d;
property NewDate: TDateTime
read f;
constructor Create(const n : string;
const d : integer;
const f : TDateTime);
end;
Sería de gran ayuda que me orientaseis un poco porque soy nuevo en delphi y no tengo mucha idea.