Domanda

I'm writing a small FireMonkey-App with Embarcadero Delphi XE5 for OS X (testing in 10.9 on my iMac) to read a text file from a Samba "drive" which is connected to my Win7 machine. The file is accessible by double clicking it but TStringList doesn't seem to be able to load it. I do not get any error message (not even an exception). After calling LoadFromFile() the Text attribute is empty.

I tried TStringList, TStringStream (with DataString property), TFileStream and FileOpen()/FileRead()/FileClose(). The first two seems to be empty after loading. TFileStream doesn't read data or reads garbage (maybe I just wrote it wrong). However FileOpen()/FileRead() works fine. Is it possible to use TStringList in FireMonkey applications or is this some kind of limitation for multi-platform applications?

PS: I tried to mount my shared folder as Guest and as the administrator user of the Win7 machine.

Here is the example code I used to test it:

procedure TForm1.Button2Click(Sender: TObject);
var
  sl: TStringList;
  ss: TStringStream;
  fs: TFileStream;
  b: array[0..20480] of char;
  sFile: String;

  FileHandle : Integer;
  iFileLength: Integer;
  Buffer: PAnsiChar;
  iBytesRead: Integer;
begin
  sFile := '/Volumes/freigabe/aida64_2013-12-13_18-50-09_log.csv';

  sl := TStringList.Create();
  sl.LoadFromFile(sFile);
  ShowMessage(sl.Text);  // Empty

  ss := TStringStream.Create();
  ss.LoadFromFile(sFile);
  ShowMessage(ss.DataString);  // Empty

  fs := TFileStream.Create(sFile, fmOpenRead);
  fs.Read(b, fs.Size);
  ShowMessage(AnsiString(b));  // Garbage data - I think I do it wrong with Read() above...

  // Example from documentation
  FileHandle := SysUtils.FileOpen(sFile, fmOpenRead);
  if FileHandle > 0 then
    begin
      try
        iFileLength := SysUtils.FileSeek(FileHandle,0,2);
        FileSeek(FileHandle, 0, 0);
        Buffer := PAnsiChar(System.AllocMem(iFileLength + 1));
        iBytesRead := SysUtils.FileRead(FileHandle, Buffer^, iFileLength);
      finally
        FreeMem(Buffer);

      ShowMessage(Buffer);  // OK!
      end;
    end
    else
      ShowMessage('Error');


  sl.Free;
  ss.Free;
  fs.Free;
end;
È stato utile?

Soluzione

I had a same issue with LoadFromFile also with local files. The Text attribute was empty all the time. It worked after I called the method with the Encoding parameter:

htmlFile := TStringList.Create;    
htmlFile.LoadFromFile(sourceFile, TEncoding.ASCII);

Hopefully this will help you too.

Andy

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top