Recent

Author Topic: [SOLVED] Bizarre File Access Problem with TFileStream  (Read 1790 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
[SOLVED] Bizarre File Access Problem with TFileStream
« on: November 09, 2020, 03:55:48 pm »
Sorry to resort to asking this, as the answer must surely be an obvious one I am missing.

I am using TFileListBox with Lazarus 2.0.10 (also tried with 2.0.4) to show files in a folder to the user.

When the user clicks a file, I want to load the content of it into TMemoryStream and show it in Memo1. Super simple.

Yet for some reason, the program says it is unable to open the file.

Code: [Select]
Unable to open file "C:\temp\test.html": The system cannot find the file specified.

Press OK to ignore and risk data corruption.
Press Abort to kill the program.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileListBox1Click(Sender: TObject);
  2. var
  3.   EncodedStream : TFileStream;
  4.   SelectedFile : string;
  5. begin
  6.   SelectedFile := ExpandFileName(Filelistbox1.Items[FileListBox1.ItemIndex]); // THIS WAY CAUSES THE ERROR TO BE RAISED BELOW
  7.   // SelectedFile := 'C:\temp\test.html'; // YET IF I HARD CODE THE EXACT SAME PATH, AN ERROR IS NOT RAISED BELOW
  8.   try
  9.   EncodedStream := TFileStream.Create(SelectedFile, fmOpenRead or fmShareDenyNone);  //*** ERROR IS RAISED HERE ***
  10.   EncodedStream.Position := 0;
  11.   Memo1.Lines.LoadFromStream(EncodedStream);    
  12. ...
  13.  

What on Earth am I missing? I've used ShowMessage(SelectedFile) and it returns exactly the right path and name. Are any of you able to download the attached sample project and get it to display a selected non-binary file in Memo1?
« Last Edit: November 09, 2020, 06:08:27 pm by Gizmo »

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Bizarre File Access Problem with TFileStream
« Reply #1 on: November 09, 2020, 05:18:00 pm »
Does it work if you use TrimAndExpandFilename() (in package LazUtils, unit LazFileUtils) instead of ExpandFilename()?

It shoudn't matter but it's worth testing it, in case there is any strange, non-visible character in the name as returned by the filelistbox
« Last Edit: November 09, 2020, 05:20:18 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Bizarre File Access Problem with TFileStream
« Reply #2 on: November 09, 2020, 05:22:35 pm »
Thanks for the input, but no sir. I get a project exception now but on same line still.

Are you able to try the project attached? I can't work out if it is just my computer or not.

Sieben

  • Sr. Member
  • ****
  • Posts: 390
Re: Bizarre File Access Problem with TFileStream
« Reply #3 on: November 09, 2020, 05:37:11 pm »
When I run that project, ExpandFileName results in a filename composed of the current directory and the filename in the listbox. Did you try sth like

Code: Pascal  [Select][+][-]
  1. SelectedFile := AppendpathDelim(FileListBox1.Directory)+(Filelistbox1.Items[FileListBox1.ItemIndex]);
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Bizarre File Access Problem with TFileStream
« Reply #4 on: November 09, 2020, 05:39:38 pm »
Well how odd.

Yes, thank you, that has worked. But, I have no understanding why because the debugger reported correct path and name

Sieben

  • Sr. Member
  • ****
  • Posts: 390
Re: Bizarre File Access Problem with TFileStream
« Reply #5 on: November 09, 2020, 05:47:31 pm »
I don't have a clue. But I would expect it to work like that since ExpandFileName has no knowledge of the FileListBox and it's settings.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Sieben

  • Sr. Member
  • ****
  • Posts: 390
Re: [SOLVED] Bizarre File Access Problem with TFileStream
« Reply #6 on: November 10, 2020, 01:53:15 am »
Just to bring this to a proper end - TFileListBox has a public property named FileName, so:

Code: Pascal  [Select][+][-]
  1. SelectedFile := FileListBox1.FileName;
     
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

 

TinyPortal © 2005-2018