Incorrect txt file reading

2

Well I'm reading the following information from a txt

0016009993|GuilhermeLima|Azul|4|21|basico+completo|6

After reading I am separating the contents using the delimiters "|" to separate in memos, with the code below.

  begin
    Linha.Delimiter:='|';
    Linha.DelimitedText:=Texto[i];
    cod:=Linha.Strings[0];
    nome:=Linha.Strings[1];
    cor:=Linha.Strings[2];
    codv := linha.strings[3];
    idadade:=Linha.Strings[4];
    tipo:=Linha.Strings[5];
    id:=Linha.Strings[6];
    try
      memo1.lines.add(cod);
      memo2.lines.add(nome);
      memo3.lines.add(cor);
      memo7.Lines.Add(codv);
      memo4.Lines.Add(idade);
      memo5.Lines.Add(tipo);
      memo6.Lines.Add(id);
    except
      ShowMessage('Erro!!! A base de dados não está no padrão correto!');

So that's fine, but if txt is this way

0016009993|Guilherme Lima|Azul|4|21|basico+completo|6

(a space in the name, which would be correct)

values go wrong for memos, why does this occur?

    
asked by anonymous 23.12.2015 / 12:34

2 answers

2

If you were using a current version of Delphi you could use Linha.StrictDelimiter , but in the is not possible.

Alternatively, you can do this:

linha.DelimitedText := '"' + StringReplace(Texto[i], linha.Delimiter, '"' + linha.Delimiter + '"', [rfReplaceAll]) + '"';

They did this question similar in SOEn, I believe the answer is for your case.

    
23.12.2015 / 13:39
1

I had a similar problem, but I use another way and I recommend using the split procedure

procedure Split(const Delimiter: Char;Input: string; const Strings: TStrings);
begin
Assert(Assigned(Strings)) ;
Strings.Clear;
Strings.Delimiter       := Delimiter; 
strings.StrictDelimiter := True;
Strings.DelimitedText   := Input;
end; 

By leaving the "StrictDelimiter" to true it ignores the blanks and only separates the variable with the entered character.

call as follows:

Split([caracter de referencia],[variavel de origem], [stringlist que irá receber os valores]);

ie it will call the split as follows:

Split('|', '0016009993|GuilhermeLima|Azul|4|21|basico+completo|6', Linha);

And as a hint, as you are passing the values to different memos, do not save them in variables, make a FOR or a WHILE that reads the stringlist to the end and saves the value in the memo, it will give a streamlined in your work . And if you need to use this value later get straight from the memo.

I hope to have helped!

    
30.12.2015 / 14:21