简体   繁体   中英

Encoding error in Delphi 2010

I am testing Delphi 2010 and I was with the following mistake:
Do I enter with this chain of characters "096 - Construção Ltda" and do I only move her/it for another variable and do look at him/it what leaves "096 - Construção Ltda", does anybody know how to find out that that???

Entrance base

001 Alcides João Pereira
002 Alvir Maçaneiro
003 Auto Elétrica Imamura Ltda
004 Auto Peças Araújo
005 Auto Peças Porto Eixo Ltda
006 Auto Peças União
007 Azambuja Industria Comercio de Materiais de Construção Ltda
008 Balaroti Comercio De Materiais De Construção Ltda
009 Baldissera Logística e Transportes Ltda Me
010 Battistella Veículos Pesados Ltda
011 Berton Diesel Auto Peças
012 Bisolo Materiais de Construção Ltda

procedure TForm1.Button2Click(Sender: TObject);
var
   tfEntrada : TextFile;
   intI, intJ : Integer;
   strA, strS : String;

   procedure lerUm; //To read a registration of the file text
   begin
      inc( intI );
      ReadLn( tfEntrada, strS );

      strA := Copy( IntToStr( intI + 1000 ), 2, 3 ) + ' - ';
      Edit1.Text := strS;

   end;

begin

   intI := 0;
   AssignFile( tfEntrada, 'nomes_tst_0001.txt' );
   Reset( tfEntrada );

   lerUm;

   while not Eof ( tfEntrada ) do
   begin

     mmEntrada.Lines.Add( strA + strS ); //I move for TMemo(mmEntrada, mmSaida), in the form

     mmSaida.Lines.Add( strA + strS );

     lerUm;

  end;

  CloseFile( tfEntrada );

end;

result base

001 - Alcides João Pereira
002 - Alvir Maçaneiro
003 - Auto Elétrica Imamura Ltda
004 - Auto Peças Araújo
005 - Auto Peças Porto Eixo Ltda
006 - Auto Peças União
007 - Azambuja Industria Comercio de Materiais de Construção Ltda
008 - Balaroti Comercio De Materiais De Construção Ltda
009 - Baldissera LogÃstica e Transportes Ltda Me
010 - Battistella VeÃculos Pesados Ltda
011 - Berton Diesel Auto Peças 012 - Bisolo Materiais de Construção Ltda

Do not use AssignFile. It is a legacy code and it does not work with UnicodeStrings. Instead, use a TStringList or a TFileStream to read the file.

[untested]

procedure ReadFile;  
var  
  vFileReader : TstringList;  
begin  
  vFileReader := TStringList.Create;  
  try
    vFileReader.LoadFromFile('nomes_tst_0001.txt');  
    mmEntrada.Lines.Assign(vFileReader);  
  finally
    vFileReader.Free;  
  end;
end;

EDITED

Another nice solution is the follow function i wrote a long time ago:

[tested]

function GetFileAsString(aFileName: string; aOffSet : Integer = 0; aChunkSize: Integer = -1): string;
var
  vStream: TFileStream;
  vBuffer: TBytes;
  vCurEncoding, vDefEncoding: TEncoding;
  vOffSet: Integer;
  vFileSize: Int64;
begin
  vCurEncoding := nil;
  vDefEncoding := TEncoding.Default;
  vStream := TFileStream.Create(aFileName, fmOpenRead + fmShareDenyNone);
  try
    if aChunkSize > 0 then begin
      vFileSize := aChunkSize;
    end
    else begin
      vFileSize := vStream.Size;
    end;
    vStream.Position := aOffSet;
    SetLength(vBuffer, vFileSize);
    vStream.ReadBuffer(Pointer(vBuffer)^, vFileSize);
    vOffSet := TEncoding.GetBufferEncoding(vBuffer, vCurEncoding);
    if (vCurEncoding <> vDefEncoding) then begin
      vBuffer := TEncoding.Convert(vCurEncoding, vDefEncoding, vBuffer, vOffSet,   vFileSize - vOffSet);
    end;
    Result := vDefEncoding.GetString(vBuffer);
  finally
    vStream.Free;
  end;
end;

This function is capable of handling unicode strings (with BOM) and also ansistring. Actually, it can read all kind of text files you have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM