DarthGomis
04-02-2019, 20:23:23
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.
procedure TForm1.CreateList(fileName: string);
var
tempLine: string; //Line with all data
line: TLine; //Line with data fragmented
cha: char;
Data: TextFile;
tipe: string; //Tipe of data
temp: string; //temporally data to storage
nameData: string; //name data
numberData: integer; //number data
dateData: TDateTime;
lineData: integer; //date data
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);
//Select tipe of data
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
//ShowMessage(tipe);
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
//ShowMessage(temp);
dateData := StrToDate(temp);
temp:='';
except
//ShowMessage('puto');
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
//ShowMessage(DateToStr(dateData));
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
TLine = class
//Data fields of a line
private
n: string; //N:
d: integer; //D:
f: TDateTime; //F:
//Properties to read the data values
public
line : string;
property NewName: string
read n;
property NewNumber: integer
read d;
property NewDate: TDateTime
read f;
//Constructor
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.
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.
procedure TForm1.CreateList(fileName: string);
var
tempLine: string; //Line with all data
line: TLine; //Line with data fragmented
cha: char;
Data: TextFile;
tipe: string; //Tipe of data
temp: string; //temporally data to storage
nameData: string; //name data
numberData: integer; //number data
dateData: TDateTime;
lineData: integer; //date data
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);
//Select tipe of data
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
//ShowMessage(tipe);
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
//ShowMessage(temp);
dateData := StrToDate(temp);
temp:='';
except
//ShowMessage('puto');
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
//ShowMessage(DateToStr(dateData));
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
TLine = class
//Data fields of a line
private
n: string; //N:
d: integer; //D:
f: TDateTime; //F:
//Properties to read the data values
public
line : string;
property NewName: string
read n;
property NewNumber: integer
read d;
property NewDate: TDateTime
read f;
//Constructor
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.